运行springboot的测试遇到这个问题,大致就是junit版本太低,需要提升版本
<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>
<!--去除低版本的junit-->
<exclusion>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</exclusion>
</exclusions>
</dependency>
<!--引入高版本的junit-->
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<scope>test</scope>
</dependency>
同时需要引入junit5 的类
- 把@Test的import 由JUnit 4的org.junit.Test改為JUnit 5的org.junit.jupiter.api.Test
- 把JUnt 4的@Ignore改為@Disabled
- 把JUnit 4的org.junit.Assert改為JUnit 5的org.junit.jupiter.api.Assertions。
- 移除測試類別上的@RunWith(SpringRunner.class)。因為Sprinb Boot執行JUnit 5要改用@ExtendWith(SpringExtension.class),而@SpringBootTest中已經有整合了