Spring Boot(十二)单元测试JUnit

1、介绍

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

<!--more-->git

  • 白盒测试:把测试对象看做一个打开的盒子,程序内部的逻辑结构和其余信息对测试人 员是公开的;程序员

  • 回归测试:软件或环境修复或更正后的再测试;github

  • 单元测试:最小粒度的测试,以测试某个功能或代码块。通常由程序员来作,由于它须要知道内部程序设计和编码的细节;web

JUnit GitHub地址:https://github.com/junit-teamspring

2、JUnit使用

开发环境:数据库

  • Spring Boot 2.0.4 RELEASE
  • JUnit 4.12
  • Maven
  • IDEA 2018.2

2.1 检测JUnit依赖

若是是Spring Boot项目默认已经加入了JUnit框架支持,可在pom.xml中查看:数组

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-test</artifactId>
    <scope>test</scope>
</dependency>

若是Maven项目中没有添加JUnit依赖,可参照如上代码,手动添加。框架

2.2 基础使用

简单的测试代码以下:spring-boot

@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

代码示例以下:

@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); //验证不经过,string二、string3指向的引用不一样
}

2.5 Web模拟测试

在Spring Boot项目里面能够直接使用JUnit对web项目进行测试,Spring 提供了“TestRestTemplate”对象,使用这个对象能够很方便的进行模拟请求。

Web测试只须要进行两步操做:

  1. 在@SpringBootTest注解上设置“ebEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT”随机端口;
  2. 使用TestRestTemplate进行post或get请求;

示例代码以下:

@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”便可,这样既能够测试数据操做方法,又不会污染数据库了。

示例代码以下:

@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按钮,就生成了对应的测试代码,用户只须要完善框架里面的具体测试逻辑就能够了。

相关文章
相关标签/搜索