junit4 VS junit5 在Springboot下使用的区别
区别 | junit4 | junit5 |
---|---|---|
@SpringBootTest | 需要 | 需要 |
@RunWith(SpringRunner.class) | 需要 | 不需要 |
类可见性 | 必须public | 可默认包可见 |
方法可见性 | 必须public | 可默认包可见 |
Maven
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
- JUnit Platform: 这是Junit提供的平台功能模块,通过它,其它的测试引擎都可以接入Junit实现接口和执行。
- JUnit JUpiter:这是JUnit5的核心,是一个基于JUnit Platform的引擎实现,它包含许多丰富的新特性来使得自动化测试更加方便和强大。
JUnit Vintage:这个模块是兼容JUnit3、JUnit4版本的测试引擎,使得旧版本的自动化测试也可以在JUnit5下正常运行。
常用注解
@BeforeEach:在每个单元测试方法执行前都执行一遍
- @BeforeAll:在每个单元测试方法执行前执行一遍(只执行一次)
- @DisplayName(“商品入库测试”):用于指定单元测试的名称
- @Disabled:当前单元测试置为无效,即单元测试时跳过该测试
- @RepeatedTest(n):重复性测试,即执行n次
- @ParameterizedTest:参数化测试,
- @ValueSource(ints = {1, 2, 3}):参数化测试提供数据