JUnit5单元测试
1、JUnit5 的变化
1、Spring Boot 2.2.0 版本开始引入 JUnit 5 作为单元测试默认库
作为最新版本的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的测试引擎。
2、注意:
JUnit 5’s Vintage Engine Removed from spring-boot-starter-test,如果需要继续兼容junit4需要自行引入vintage
SpringBoot 2.4 以上版本移除了默认对 Vintage 的依赖。如果需要兼容junit4需要自行引入(不能使用junit4的功能 @Test)
springboot对junit的讲解
<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>
springboot关于junt测试的依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
3、现在版本junit测试的注解
@SpringBootTest
class Boot05WebAdminApplicationTests {
@Test
void contextLoads() {
}
}
4、以前版本关于junit测试的注解:
@SpringBootTest + @RunWith(SpringTest.class)
5、SpringBoot整合Junit以后。
• 编写测试方法:@Test标注(注意需要使用junit5版本的注解)
• Junit类具有Spring的功能,@Autowired、比如 @Transactional 标注测试方法,测试完成后自动回滚
2、JUnit5常用注解
JUnit5的注解与JUnit4的注解有所变化
junit注解官方参考文档
- @Test :表示方法是测试方法。但是与JUnit4的@Test不同,他的职责非常单一不能声明任何属性,拓展的测试将会由Jupiter提供额外测试
- @ParameterizedTest :表示方法是参数化测试,下方会有详细介绍
- @RepeatedTest :表示方法可重复执行,下方会有详细介绍
将test3方法重复测试5次:
-
@DisplayName :为测试类或者测试方法设置展示名称
-
@BeforeEach :表示在每个单元测试之前执行
-
@AfterEach :表示在每个单元测试之后执行
-
@BeforeAll :表示在所有单元测试之前执行
-
@AfterAll :表示在所有单元测试之后执行
import org.junit.jupiter.api.*;
@DisplayName("Junit5功能测试")
public class Junit5Test {
@DisplayName("测试displayName注解")
@Test
void testDisplayName(){
System.out.println(1);
}
@DisplayName("测试方法2")
@Test
void test2(){
System.out.println(2);
}
@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("所有测试结束了");
}
}
- @Tag :表示单元测试类别,类似于JUnit4中的@Categories
- @Disabled :表示测试类或测试方法不执行,类似于JUnit4中的@Ignore
- @Timeout :表示测试方法运行如果超过了指定时间将会返回错误
/**
* 设置如果测试方法执行时长超过500毫秒,提醒超时
* @throws InterruptedException
*/
@Timeout(value = 500,unit = TimeUnit.MILLISECONDS)
@DisplayName("测试方法2")
@Test
void testTimeout() throws InterruptedException {
//线程睡眠600毫秒
Thread.sleep(600);
}
- @ExtendWith :为测试类或测试方法提供扩展类引用