参考文档:
JUnit 5 Jupiter API
junit4和junit5的注解比较
JUnit 5 | JUnit 4 | 说明 |
---|---|---|
@Test | @Test | 被注解的方法是一个测试方法。与 JUnit 4 相同。 |
@BeforeAll | @BeforeClass | 被注解的(静态)方法将在当前类中的所有 @Test 方法前执行一次。 |
@BeforeEach | @Before | 被注解的方法将在当前类中的每个 @Test 方法前执行。 |
@AfterEach | @After | 被注解的方法将在当前类中的每个 @Test 方法后执行。 |
@AfterAll | @AfterClass | 被注解的(静态)方法将在当前类中的所有 @Test 方法后执行一次。 |
@Disabled | @Ignore | 被注解的方法不会执行(将被跳过),但会报告为已执行。 |
spring5集成junit4
@RunWith(SpringJUnit4ClassRunner.class) //单元测试框架 使用注解
@ContextConfiguration("classpath:bean.xml")//加载配置文件
或者
@RunWith(SpringRunner.class) //SpringRunner继承自SpringJUnit4ClassRunner
@ContextConfiguration("classpath:bean.xml")//加载配置文件
spring5集成junit5
@ExtendWith(SpringExtension.class)//指示JUnit Jupiter(Junit5)使用Spring支持扩展测试。
@ContextConfiguration("classpath:applicationContext.xml")//加载配置文件
或者
@SpringJUnitConfig(locations = "classpath:applicationContext.xml")
springboot2集成junit4
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
@Test 用org.junit.Test 配合@RunWith使用
springboot2集成junit5
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>5.7.0</version>
<scope>test</scope>
</dependency>
或者
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
包含了junit-jupiter
@Test 用org.junit.jupiter.api.Test