好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

Spring Boot 单元测试JUnit的实践

一、介绍

junit是一款优秀的开源java 单元测试 框架,也是目前使用率最高最流行的测试框架,开发工具eclipse和idea对junit都有很好的支持,junit主要用于白盒测试和回归测试。

<!--more-->

白盒测试:把测试对象看作一个打开的盒子,程序内部的逻辑结构和其他信息对测试人 员是公开的; 回归测试:软件或环境修复或更正后的再测试; 单元测试:最小粒度的测试,以测试某个功能或代码块。一般由程序员来做,因为它需要知道内部程序设计和编码的细节;

junit github地址: https://github测试数据/junit-team

二、junit使用

开发环境:

spring boot 2.0.4 release junit 4.12 maven idea 2018.2

2.1 检测junit依赖

如果是spring boot项目默认已经加入了junit框架支持,可在pom.xml中查看:

?

1

2

3

4

5

<dependency>

   <groupid>org.springframework.boot</groupid>

   <artifactid>spring-boot-starter-test</artifactid>

   <scope>test</scope>

</dependency>

如果maven项目中没有添加junit依赖,可参照如上代码,手动添加。

2.2 基础使用

简单的测试代码如下:

?

1

2

3

4

5

6

7

8

9

@runwith (springrunner. class )

@springboottest

public class simpletest {

   @test

   public void dotest() {

     int num = new integer( 1 );

     assert .assertequals(num, 1 );

   }

}

在测试类中邮件运行项目,效果如下:

从控制台可以看出测试通过了。

2.3 注解说明

2.3.1 注解列表

@runwith:标识为junit的运行环境; @springboottest:获取启动类、加载配置,确定装载spring boot; @test:声明需要测试的方法; @beforeclass:针对所有测试,只执行一次,且必须为static void; @afterclass:针对所有测试,只执行一次,且必须为static void; @before:每个测试方法前都会执行的方法; @after:每个测试方法前都会执行的方法; @ignore:忽略方法;

2.3.2 超时测试

代码如下,给test设置timeout属性即可,时间单位为毫秒:

@test(timeout = 1000)

2.4 断言测试

断言测试也就是期望值测试,是单元测试的核心也就是决定测试结果的表达式,assert对象中的断言方法:

assert.assertequals 对比两个值相等 assert.assertnotequals 对比两个值不相等 assert.assertsame 对比两个对象的引用相等 assert.assertarrayequals 对比两个数组相等 assert.asserttrue 验证返回是否为真 assert.assertflase 验证返回是否为假 assert.assertnull 验证null assert.assertnotnull 验证非null

代码示例如下:

?

1

2

3

4

5

6

7

8

9

10

11

@test

public void dotest() {

   string[] string1 = { "1" , "2" };

   string[] string2 = string1;

   string[] string3 = { "1" , "2" };

 

   assert .assertequals(string1, string2);

   assert .assertequals(string2, string3);

   assert .assertsame(string1, string2);

   assert .assertsame(string2, string3); //验证不通过,string2、string3指向的引用不同

}

2.5 web模拟测试

在spring boot项目里面可以直接使用junit对web项目进行测试,spring 提供了[testresttemplate]对象,使用这个对象可以很方便的进行模拟请求。

web测试只需要进行两步操作:

在@springboottest注解上设置[ebenvironment = springboottest.webenvironment.random_port]随机端口; 使用testresttemplate进行post或get请求;

示例代码如下:

?

1

2

3

4

5

6

7

8

9

10

11

12

@runwith (springrunner. class )

@springboottest (webenvironment = springboottest.webenvironment.random_port)

public class usercontrollertest {

   @autowired

   private testresttemplate resttemplate;

   @test

   public void getname() {

     string name = resttemplate.getforobject( "/name" , string. class );

     system.out.println(name);

     assert .assertequals( "adam" , name);

   }

}

其中getforobject的含义代表执行get请求,并返回object结果,第二个参数设置返回结果为string类型,更多的请求方法:

getforentity:get请求,返回实体对象(可以是集合); postforentity:post请求,返回实体对象(可以是集合); postforobject:post请求,返回对象;

2.6 数据库测试

在测试数据操作的时候,我们不想让测试污染数据库,也是可以实现的,只需要添加给测试类上添加[@transactional]即可,这样既可以测试数据操作方法,又不会污染数据库了。

示例代码如下:

?

1

2

3

4

5

6

7

8

9

10

11

@test

@transactional

public void savetest() {

   user user = new user();

   user.setname( "adam" );

   user.setage( 19 );

   user.setpwd( "123456" );

   userrepository.save(user);

   system.out.println( "userid:" + user.getid());

   assert .asserttrue(user.getid()> 0 );

}

执行效果如下:

我们可以看到id有了,也测试通过了,说明数据是添加是正常的,但查看数据库发现数据里面是没有这条数据的。

如果把[@transactional]去掉的话,数据库就会正常插入了。

2.7 idea快速开启测试

在idea里面可以快速的添加测试的方法,只需要在要测试的类里面右键选择[goto]点击[test],选择你需要测试的代码,点击生成即可,如果是windows 用户可以使用默认快捷键[ctrl + shift + t],效果如下图:

选完方法之后,点击ok按钮,就生成了对应的测试代码,用户只需要完善框架里面的具体测试逻辑就可以了。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

原文链接:https://segmentfault测试数据/a/1190000016907636

查看更多关于Spring Boot 单元测试JUnit的实践的详细内容...

  阅读:12次