JUnit平台套件执行失败:NoSuchMethodError问题求助
JUnit 5套件执行
mvn test时出现NoSuchMethodError错误排查 问题场景
使用JUnit Jupiter 5.9.1编写测试套件类,搭配Maven Surefire插件3.0.0-M5执行测试时触发方法找不到的错误,相关配置如下:
1. JUnit 5套件类
@Suite @SelectClasses({ AAATests.class, BBBTests.class }) public class SanityTMTestsSuite5 { }
2. JUnit版本定义
<junit-jupiter.version>5.9.1</junit-jupiter.version>
3. Maven Surefire插件配置
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <version>3.0.0-M5</version> <configuration> <testFailureIgnore>false</testFailureIgnore> <includes> <include>**/SanityTMTestsSuite*.java</include> </includes> </configuration> <dependencies> <dependency> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter</artifactId> <version>${junit-jupiter.version}</version> </dependency> <dependency> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter-suite</artifactId> <version>1.9.1</version> </dependency> <dependency> <groupId>org.junit.vintage</groupId> <artifactId>junit-vintage-engine</artifactId> <version>${junit-jupiter.version}</version> </dependency> </dependencies> </plugin>
4. 额外测试依赖
<dependency> <groupId>org.junit.platform</groupId> <artifactId>junit-platform-suite</artifactId> <version>1.9.1</version> <scope>test</scope> </dependency>
执行错误信息
org.junit.platform.commons.JUnitException: 测试引擎ID 'junit-platform-suite' 执行测试失败
引起原因: java.lang.NoSuchMethodError: 'org.junit.platform.launcher.core.LauncherDiscoveryResult org.junit.platform.launcher.core.LauncherDiscoveryResult.withRetainedEngines(java.util.function.Predicate)'
问题原因与修复方案
这个错误核心是JUnit平台组件版本不兼容:
- JUnit Jupiter 5.9.1对应JUnit Platform版本为1.9.1,但Surefire插件3.0.0-M5属于里程碑版本,自带的JUnit Platform Launcher版本和指定的1.9.1存在冲突。
- 另外,
junit-jupiter-suite是旧版套件依赖,从JUnit Platform 1.7开始,套件功能已迁移到org.junit.platform:junit-platform-suite和org.junit.platform:junit-platform-suite-api,无需重复引入。
修复步骤:
- 移除Surefire插件中的
junit-jupiter-suite依赖,避免重复依赖导致冲突。 - 升级Surefire插件到稳定版本(如3.2.2),减少里程碑版本的兼容性问题。
- 确保所有JUnit Platform相关依赖版本统一为1.9.1,和JUnit Jupiter 5.9.1匹配。
调整后的Surefire插件配置:
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <version>3.2.2</version> <configuration> <testFailureIgnore>false</testFailureIgnore> <includes> <include>**/SanityTMTestsSuite*.java</include> </includes> </configuration> <dependencies> <dependency> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter</artifactId> <version>${junit-jupiter.version}</version> </dependency> <dependency> <groupId>org.junit.vintage</groupId> <artifactId>junit-vintage-engine</artifactId> <version>${junit-jupiter.version}</version> </dependency> </dependencies> </plugin>
补充必要的套件API依赖(如果未添加):
<dependency> <groupId>org.junit.platform</groupId> <artifactId>junit-platform-suite</artifactId> <version>1.9.1</version> <scope>test</scope> </dependency> <dependency> <groupId>org.junit.platform</groupId> <artifactId>junit-platform-suite-api</artifactId> <version>1.9.1</version> <scope>test</scope> </dependency>
内容的提问来源于stack exchange,提问作者TalClip
相关产品推荐
相关产品推荐

