在 Spring 項目中運行測試的時候,得到錯誤:
TestEngine with ID 'junit-vintage' failed to discover tests” with Spring
這個錯誤的原因是 JUnit 的引擎,使用了 junit-vintage 引擎。
junit-vintage 是 Junit 4 中使用的引擎,如果你的項目使用了 Junit 5 的話,你需要在 spring-boot-starter-test 中將 JUnit 4 的引擎從測試中刪除。
<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-vintage-engine 和 junit-jupiter-engine 有什么不同
junit-vintage-engine
是 JUnit 4 中使用的測試引擎。junit-jupiter-engine
是 JUnit 5 中使用的測試引擎。
如果你的 Spring 項目使用的新的 Spring Boot 版本的話,你應該默認使用了 JUnit 5 的引擎,因此為了兼容性,你需要在 spring-boot-starter-test 這個 POM 引用的時候將 JUnit 4 的引擎去除掉。
上面的這個配置你可以嘗試下能解決你的問題。