运行JUnit5BestPractices Recipe时出现NoSuchMethodError错误求助
解决OpenRewrite升级JUnit5时的NoSuchMethodError错误
错误原因
这个NoSuchMethodError是因为OpenRewrite插件与测试框架recipe的版本不兼容导致的:你使用的rewrite-maven-plugin:4.43.0和rewrite-testing-frameworks:RELEASE(最新版)之间存在API差异,UsesType构造方法的签名在新版本中已变更,但插件依赖的旧版本类库没有这个方法。
解决方案
方案1:指定兼容的recipe版本
找到与插件版本匹配的rewrite-testing-frameworks版本,替换命令中的RELEASE。例如,针对rewrite-maven-plugin:4.43.0,可以使用1.37.0版本(该版本与插件4.43.0兼容):
mvn -U org.openrewrite.maven:rewrite-maven-plugin:4.43.0:run \ -Drewrite.recipeArtifactCoordinates=org.openrewrite.recipe:rewrite-testing-frameworks:1.37.0 \ -Drewrite.activeRecipes=org.openrewrite.java.testing.junit5.JUnit5BestPractices -e
方案2:使用最新版插件并匹配recipe版本
直接使用最新版的rewrite-maven-plugin,同时指定对应兼容的rewrite-testing-frameworks版本,示例命令:
mvn -U org.openrewrite.maven:rewrite-maven-plugin:LATEST:run \ -Drewrite.recipeArtifactCoordinates=org.openrewrite.recipe:rewrite-testing-frameworks:LATEST \ -Drewrite.activeRecipes=org.openrewrite.java.testing.junit5.JUnit5BestPractices -e
方案3:在pom.xml中配置插件(推荐)
避免命令行版本不一致问题,直接在项目pom.xml中配置OpenRewrite插件,统一管理版本:
<build> <plugins> <plugin> <groupId>org.openrewrite.maven</groupId> <artifactId>rewrite-maven-plugin</artifactId> <version>4.43.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.37.0</version> </dependency> </dependencies> </plugin> </plugins> </build>
配置完成后,执行以下命令运行:
mvn rewrite:run -e
内容的提问来源于stack exchange,提问作者Nagendra Busam
相关产品推荐
相关产品推荐

