升级至JUnit 5.8.0搭配Maven Surefire 3.0.0-M5出现NoSuchMethodError
问题根因
该错误是JUnit 5.8.0与Maven Surefire 3.0.0-M5内置的JUnit Platform版本不兼容导致的:
JUnit Jupiter 5.8.0要求最低使用JUnit Platform 1.8.0版本,而Surefire 3.0.0-M5默认绑定的是更低版本的Platform组件,导致AnnotationUtils类的方法签名不匹配,抛出NoSuchMethodError异常,进而无法识别测试引擎。
解决方案
以下三种方案任选其一即可解决问题:
方案1:修改Surefire插件配置,排除内置低版本Platform依赖,显式引入匹配版本
在pom.xml的build/plugins节点下调整Surefire插件配置:<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <version>${surefire-plugin.version}</version> <dependencies> <dependency> <groupId>org.junit.platform</groupId> <artifactId>junit-platform-surefire-provider</artifactId> <version>1.8.0</version> </dependency> <dependency> <groupId>org.junit.platform</groupId> <artifactId>junit-platform-launcher</artifactId> <version>1.8.0</version> </dependency> </dependencies> </plugin>方案2:通过JUnit BOM统一管控所有JUnit组件版本
在dependencyManagement节点引入官方bom,自动对齐所有JUnit相关依赖的版本,从根源避免版本冲突:<dependencyManagement> <dependencies> <dependency> <groupId>org.junit</groupId> <artifactId>junit-bom</artifactId> <version>5.8.0</version> <scope>import</scope> <type>pom</type> </dependency> </dependencies> </dependencyManagement>引入后无需为各个JUnit组件单独声明版本,会自动和bom版本保持一致。
方案3:升级Maven Surefire插件版本
3.0.0-M7及以上版本的Surefire插件已经更新了内置的JUnit Platform依赖,兼容JUnit 5.8.0+版本,直接修改pom中surefire-plugin.version属性值为3.0.0-M7或更新的正式版即可。
内容的提问来源于stack exchange,提问作者Rob
相关产品推荐
相关产品推荐

