当 Spring Boot 应用程序从 2.1.5 迁移到 2.4

问题描述

我有一个在 spring-boot 2.1.5 中运行的应用程序,将其迁移到版本 2.4.0。我的测试现在都没有执行。 当我点击 mvn clean test 时,它总是说执行了 0 个测试。

我注意到 spring boot 2.4.0 与 junit.jupiter 库捆绑在一起。我的 2.1.5 测试使用的是 junit:junit:4.4 依赖项。

如何在不迁移到 junit.jupiter 的情况下保留我的旧测试用例?

解决方法

这在 release notes for Spring Boot 2.4 中有介绍:

如果您不想将测试迁移到 JUnit 5 并希望继续使用 JUnit 4,请添加对 Vintage Engine 的依赖项,如下面的 Maven 示例所示:

<dependency>
   <groupId>org.junit.vintage</groupId>
   <artifactId>junit-vintage-engine</artifactId>
   <scope>test</scope>
   <exclusions>
       <exclusion>
           <groupId>org.hamcrest</groupId>
           <artifactId>hamcrest-core</artifactId>
       </exclusion>
   </exclusions>
</dependency>

如果您使用的是 Gradle,则等效配置如下例所示:

testImplementation("org.junit.vintage:junit-vintage-engine") {
   exclude group: "org.hamcrest",module: "hamcrest-core"
}