JUnit5
前言
版本说明
junit5=5.7.0
相关链接:
- Junit5 官方地址:https://junit.org/junit5/
- Junit5 官网文档地址:https://junit.org/junit5/docs/current/user-guide/
- junit-jupiter maven 地址:https://mvnrepository.com/artifact/org.junit.jupiter/junit-jupiter
源码地址
- Github:
- Gitee:
JUnit5 介绍
JUnit 5 = JUnit Platform + JUnit Jupiter + JUnit Vintage
- JUnit Platform: 可作为一个基础发射测试框架在JVM上。它还定义了
TestEngine用于开发在平台上运行的测试框架的API。 - JUnit Jupiter: 是新的编程模型和 扩展模型的组合,用于在JUnit 5中编写测试和扩展。Jupiter子项目提供了一个
TestEngine在平台上运行基于Jupiter的测试的功能。 - JUnit Vintage: 提供了一个
TestEngine在平台上运行基于JUnit 3和JUnit 4的测试的功能。
POM核心依赖
JUnit Jupiter 聚合器工件,可传递性地获取 junit-jupiter-api,junit-jupiter-params和 junit-jupiter-engine的依赖关系,并在诸如 Gradle 和 Maven 之类的构建工具中简化了依赖关系管理。
<!-- https://mvnrepository.com/artifact/org.junit.jupiter/junit-jupiter -->
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>5.7.0</version>
<scope>test</scope>
</dependency>
常用注解
| 注解 | 说明 |
|---|---|
| @BeforeAll | 必须要是static方法。 只执行一次,执行时机是在所有测试和 @BeforeEach 注解方法之前 |
| @BeforeEach | 在每个测试执行之前执行 |
| @Test | 测试方法 |
| @AfterEach | 在每个测试执行之后执行。 |
| @AfterAll | 必须要是static方法。 只执行一次,执行时机是在所有测试和 @AfterEach 注解方法之后。 |
| @RepeatedTest(value = N) | 重复N次执行测试 |
| @DisplayName(“测试显示名”) | 声明测试类或测试方法的自定义显示名称 |
| @Disabled | 用于禁用测试类或测试方法 |
| @Nested | 表示带注释的类是一个非静态的嵌套测试类 |
| @TestMethodOrder | 测试类排序方式: DisplayName:根据测试方法的显示名称按字母顺序对它们进行排序; OrderAnnotation:根据@Order值排序 |
实践指南
基本示例
package top.simba1949;
import org.junit.jupiter.api.*;
/**
* @author SIMBA1949
* @date 2020/10/17 19:06
*/
public class AppOneTest {
@BeforeAll
public static void beforeAll(){
System.out.println("beforeAll");
}
@BeforeEach
public void beforeEach(){
System.out.println("beforeEach");
}
@Test
public void testCore(){
System.out.println("testCore");
}
@AfterEach
public void afterEach(){
System.out.println("afterEach");
}
@AfterAll
public static void afterAll(){
System.out.println("afterAll");
}
}
输出结果

@RepeatedTest 示例
@RepeatedTest(value = 1) :value 表示再重复次数
package top.simba1949;
import org.junit.jupiter.api.*;
/**
* @author SIMBA1949
* @date 2020/10/17 19:10
*/
public class AppTwoTest {
@BeforeAll
public static void beforeAll(){
System.out.println("beforeAll");
}
@BeforeEach
public void beforeEach(){
System.out.println("beforeEach");
}
@RepeatedTest(value = 1)
@Test
public void testCore(){
System.out.println("testCore");
}
@AfterEach
public void afterEach(){
System.out.println("afterEach");
}
@AfterAll
public static void afterAll(){
System.out.println("afterAll");
}
}

@DisplayName
package top.simba1949;
import org.junit.jupiter.api.*;
/**
* @author SIMBA1949
* @date 2020/10/17 19:15
*/
@DisplayName(value = "DisplayName-AppThreeTest")
public class AppThreeTest {
@BeforeAll
public static void beforeAll(){
System.out.println("beforeAll");
}
@BeforeEach
public void beforeEach(){
System.out.println("beforeEach");
}
@Test
@DisplayName(value = "DisplayName-testCore")
public void testCore(){
System.out.println("testCore");
}
@AfterEach
public void afterEach(){
System.out.println("afterEach");
}
@AfterAll
public static void afterAll(){
System.out.println("afterAll");
}
}
未添加 @DisplayName 注解前,打印如下 :

添加 @DisplayName 注解后,打印如下:

@Disabled
@Disabled 是禁用某个测试类或者方法
package top.simba1949;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
/**
* @author SIMBA1949
* @date 2020/10/17 19:23
*/
public class AppFourTest {
@Test
@Disabled
public void testMethodOne(){
System.out.println("testMethodOne");
}
@Test
public void testMethodTwo(){
System.out.println("testMethodTwo");
}
}

@Nested
@Nested 表示带注释的类是一个非静态的嵌套测试类
package top.simba1949;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
/**
* @author SIMBA1949
* @date 2020/10/17 19:27
*/
public class AppFiveTest {
@Nested
class AppNestedFiveTest{
@Test
public void appNestedFiveTestMethod(){
System.out.println("appNestedFiveTestMethod");
}
}
}
@TestMethodOrder
@TestMethodOrder 测试类排序方式注解,常用排序方式:
- DisplayName:根据测试方法的显示名称按字母顺序对它们进行排序;
- OrderAnnotation:根据
@Order值排序,@Order整数,数值越小越先知心;
package top.simba1949;
import org.junit.jupiter.api.MethodOrderer;
import org.junit.jupiter.api.Order;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestMethodOrder;
/**
* @author SIMBA1949
* @date 2020/10/17 19:29
*/
@TestMethodOrder(value = MethodOrderer.OrderAnnotation.class)
public class AppSixTest {
@Test
@Order(-2)
public void methodA(){
System.out.println("methodA");
}
@Test
@Order(-10)
public void methodB(){
System.out.println("methodB");
}
}
834

被折叠的 条评论
为什么被折叠?



