将多个Junit4测试迁移到Junit5的一种方法是使用OpenRewrite。
它将具有多个预定义的配方,用于常见任务,例如将Junit4迁移到Junit5。
如果您有一个没有Spring的Maven项目,则可以将以下内容添加到您的pom.xml
中。
<build>
<plugins>
<plugin>
<groupId>org.openrewrite.maven</groupId>
<artifactId>rewrite-maven-plugin</artifactId>
<version>4.44.0</version>
<configuration>
<activeRecipes>
<recipe>org.openrewrite.java.testing.junit5.JUnit5BestPractices</recipe>
</activeRecipes>
</configuration>
<dependencies>
<dependency>
<groupId>org.openrewrite.recipe</groupId>
<artifactId>rewrite-testing-frameworks</artifactId>
<version>1.36.0</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
简要说明:
<activeRecipes>
<recipe>org.openrewrite.java.testing.junit5.JUnit5BestPractices</recipe>
</activeRecipes>
这部分激活了重写JUnit配方,该配方位于依赖项org.openrewrite.recipe:rewrite-testing-frameworks
中。
如果您正在使用Spring或Spring-Boot,则需要激活配方org.openrewrite.java.spring.boot2.SpringBoot2JUnit4to5Migration
和以下依赖项。
<dependency>
<groupId>org.openrewrite.recipe</groupId>
<artifactId>rewrite-spring</artifactId>
<version>4.35.0</version>
</dependency>
如果您正在使用Gradle,您应该查看官方文档。
通过mvn rewrite:dryRun
,您可以在一个.patch
文件中看到结果。
通过mvn rewrite:run
进行迁移。如果您有VCS,您可以随时运行而不进行干练,因为您可以在VCS的差异工具中看到差异。