| 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> |
| <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> |
| ● @Test :表示方法是测试方法。但是与JUnit4的@Test不同,他的职责非常单一不能声明任何属性,拓展的测试将会由Jupiter提供额外测试 |
| ● @ParameterizedTest :表示方法是参数化测试 |
| ● @RepeatedTest :表示方法可重复执行 |
| ● @DisplayName :为测试类或者测试方法设置展示名称 |
| ● @BeforeEach :表示在每个单元测试之前执行 |
| ● @AfterEach :表示在每个单元测试之后执行 |
| ● @BeforeAll :表示在所有单元测试之前执行 |
| ● @AfterAll :表示在所有单元测试之后执行 |
| ● @Tag :表示单元测试类别,类似于JUnit4中的@Categories |
| ● @Disabled :表示测试类或测试方法不执行,类似于JUnit4中的@Ignore |
| ● @Timeout :表示测试方法运行如果超过了指定时间将会返回错误 |
| ● @ExtendWith :为测试类或测试方法提供扩展类引用 |
| @SpringBootTest |
| @DisplayName("junit5功能测试类") |
| public class Junit5Test { |
| |
| @DisplayName("测试前置条件") |
| @Test |
| void testassumptions(){ |
| System.out.println("111111"); |
| |
| } |
| |
| } |
- @BeforeEach、@AfterEach、@BeforeAll、@AfterAll
| @SpringBootTest |
| @DisplayName("junit5功能测试类") |
| public class Junit5Test { |
| |
| @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("所有测试以及结束了..."); |
| |
| } |
| |
| } |
| @SpringBootTest |
| @DisplayName("junit5功能测试类") |
| public class Junit5Test { |
| |
| @Disabled |
| @DisplayName("测试方法2") |
| @Test |
| void test2() { |
| System.out.println(2); |
| } |
| |
| } |
| @SpringBootTest |
| @DisplayName("junit5功能测试类") |
| public class Junit5Test { |
| |
| |
| |
| |
| |
| @Timeout(value = 500, unit = TimeUnit.MILLISECONDS) |
| @Test |
| void testTimeout() throws InterruptedException { |
| Thread.sleep(600); |
| } |
| |
| } |
| @SpringBootTest = @BootstrapWith(SpringBootTestContextBootstrapper.class) + @ExtendWith(SpringExtension.class) |
| |
| # @SpringBootTest这里接入的是spring boot提供的测试平台 |
| # 如果想接入其他测试平台,则使用@ExtendWith修改 |
| @SpringBootTest |
| @DisplayName("junit5功能测试类") |
| public class Junit5Test { |
| |
| @RepeatedTest(5) |
| @Test |
| void test3() { |
| System.out.println(5); |
| } |
| |
| } |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?