第四节:单元测试
4.1 JUnit5的变化
SpringBoot 2.2.0版本开始引入JUnit5作为单元测试默认库
作为最新版本的JUnit框架,JUnit5与之前版本的Junit框架有很大的不同。由三个不同子项目的几个不同模块组成。
JUnit 5 = JUnit Platform + JUnit Jupiter + JUnit Vintage
JUnit Platform: Junit Platform是在JVM上启动测试框架的基础,不仅支持Junit自制的测试引擎,其他测试引擎也都可以接入。
JUnit Jupiter: JUnit Jupiter提供了JUnit5的新的编程模型,是JUnit5新特性的核心。内部 包含了一个测试引擎,用于在Junit Platform上运行。
JUnit Vintage: 由于JUint已经发展多年,为了照顾老的项目,JUnit Vintage提供了兼容JUnit4.x,Junit3.x的测试引擎。
引入依赖:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
注意:
SpringBoot 2.4 以上版本移除了默认对 Vintage 的依赖。如果需要兼容junit4需要自行引入(不能使用junit4的功能 @Test)
JUnit 5’s Vintage Engine Removed from spring-boot-starter-test,如果需要继续兼容junit4需要自行引入vintage
<dependency>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-core</artifactId>
</exclusion>
</exclusions>
</dependency>
现在版本:
@SpringBootTest
class Boot05WebAdminApplication {
@Test
void contentLoads(){
}
}
以前:
@SpringBootTest+@RunWith(SpringTest.class)
SpringBoot整合Junit后:
- 编写测试方法:
@Test
标注(注意需要使用Junit5版本的注解) - Junit类具有Spring的功能,
@Autowired
、比如@Transactional
标注测试方法,测试完成后会自动回滚
4.2 Junit5常用注解
Junit5的注解和Junit4的注解有所变化:
- **@Test:**表示方法是测试方法,但是与Junit4的@Test不同,它的职责非常单一,不能声明任何属性,扩展的测试将会由Jupiter提供额外测试
- @ParameterizedTest:表示方法是参数化测试
- @RepeatedTest:表示方法可重复执行
- @DisplayName:为测试类或者测试方法设置展示名称
- @BeforeEach:表示在每个单元测试之前执行
- @AfterEach:表示在每个单元测试之后执行
- @BeforeAll:表示在所有单元测试之前执行
- @AfterAll:表示在所有单元测试之后执行
- @Tag:表示单元测试类别,类似于Junit4中的
@Categories
- @Disabled:表示测试类或测试方法不执行,类似于Junit4中的
@Ignore
- @Timeout:表示测试方法运行如果超过了指定时间将会返回错误
- @ExtendWith:为测试类或测试方法提供扩展类引用
@DisplayName("Junit5功能测试类")
/**
* @SpringBootTest注解中包含:
* @BootstrapWith(SpringBootTestContextBootstrapper.class)
* @ExtendWith({SpringExtension.class})
*/
@SpringBootTest//使用SpringBoot中的容器功能
public class Junit5Test {
@Autowired
JdbcTemplate jdbcTemplate;
@DisplayName("测试DisplayName注解")
@Test
void testDisplayName(){
System.out.println(jdbcTemplate);
System.out.println(1);
}
@Disabled//禁用此测试单元
@DisplayName("测试方法2")
@Test
void test2(){
System.out.println(2);
}
@RepeatedTest(5)//再重复测试5次
@Test
void test3(){
System.out.println(5);
}
@BeforeEach
void testBeforeEach(){
System.out.println("测试就要开始了。。。。");
}
@AfterEach
void testAfterEach(){
System.out.println("测试就要结束了。。。。");
}
@BeforeAll
static void testBeforeAll(){
System.out.println("所有测试就要开始了。。。。");
}
@AfterAll
static void testAfterAll(){
System.out.println("所有测试已经结束了。。。。");
}
/**
* 规定方法超时时间,超出时间测试抛出异常
* @throws InterruptedException
*/
@Timeout(value = 500,unit = TimeUnit.MILLISECONDS)//500毫秒超时,抛出超时异常
@Test
void testTimeOut() throws InterruptedException {
Thread.sleep(500);
}
}
4.3 断言(assertions)
断言(assertions)是测试方法中的核心部分,用来对测试需要满足的条件进行验证。这些断言方法都是org.junit.jupiter.api.Assertions
的静态方法。Junit 5 内置的断言可以分为以下几个类别:
检查业务逻辑返回的数据是否合理
所有的测试运行结束以后,会有一个详细的测试报告;
断言的情况下:前面断言失败,后面的代码都不会执行
1、简单断言
用来对单个值进行简单验证:
方法 | 说明 |
---|---|
assertEquals | 判断两个对象或两个原始类型是否相等 |
assertNotEquals | 判断两个对象或两个原始类型是否不相等 |
assertSame | 判断两个对象引用是否指向同一个对象 |
assertNotSame | 判断两个对象引用是否指向不同的对象 |
assertTrue | 判断给定的布尔值是否为 true |
assertFalse | 判断给定的布尔值是否为 false |
assertNull | 判断给定的对象引用是否为 null |
assertNotNull | 判断给定的对象引用是否不为 null |
@Test
@DisplayName("测试简单断言")
void testSimpleAssertions(){
int cal=cal(1,2);
//不相等则返回提示信息message
Assertions.assertEquals(3,cal,"业务逻辑计算失败");
Object obj1=new Object();
Object obj2=new Object();
Assertions.assertSame(obj1,obj2,"不是同一个对象");
}
2、数组断言
通过 assertArrayEquals 方法来判断两个对象或原始类型的数组是否相等
@Test
@DisplayName("测试Array断言"