JUnit 5 测试 Spring 引擎的时候提示 junit-vintage 错误

时间:2022-07-26
本文章向大家介绍JUnit 5 测试 Spring 引擎的时候提示 junit-vintage 错误,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

在 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 的引擎去除掉。

上面的这个配置你可以尝试下能解决你的问题。

https://www.ossez.com/t/junit-5-spring-junit-vintage/562