运行JUnit单元测试报java.lang.NoSuchMethodError的解决方案咨询
解决JUnit5测试中NoSuchMethodError异常的方案
我最近在运行一段简单的Kotlin单元测试时碰到了NoSuchMethodError,折腾了好一会儿才找到解决办法,正好和你遇到的情况一模一样,分享给你参考:
我的测试代码是这样的:
class ExampleUnitTest { @Test fun addition_is_Correct() { assertEquals(4, (2 + 2).toLong()) } }
运行后抛出了如下异常:
Exception in thread "main" java.lang.NoSuchMethodError: org.junit.platform.launcher.Launcher.execute(Lorg/junit/platform/launcher/LauncherDiscoveryRequest;)V at com.intellij.junit5.JUnit5IdeaTestRunner.startRunnerWithArgs(JUnit5IdeaTestRunner.java:61) at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:51) at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242) at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at com.intellij.rt.execution.application.AppMainV2.main(AppMainV2.java:131)
我当时也更新了build.gradle里的所有JUnit依赖,内容和你差不多:
testImplementation 'junit:junit:4.12' testImplementation 'org.jetbrains.spek:spek-api:1.1.5' testImplementation 'org.jetbrains.spek:spek-junit-platform-engine:1.1.5' testImplementation 'org.junit.platform:junit-platform-launcher:1.0.0' testImplementation 'org.junit.platform:junit-platform-runner:1.0.0' testImplementation 'org.junit.vintage:junit-vintage-engine:4.12.3' testImplementation 'org.junit.jupiter:junit-jupiter-api:5.0.0' testImplementation 'org.junit.jupiter:junit-jupiter-params:5.0.0' testImplementation 'org.junit.jupiter:junit-jupiter-engine:5.0.0'
问题根源
这个异常的核心原因是JUnit平台相关依赖的版本不兼容:你用的junit-platform-launcher:1.0.0是比较老的版本,和其他JUnit 5组件(比如junit-jupiter-engine:5.0.0)或者IDE的JUnit Runner版本不匹配,导致找不到对应的execute方法。
可行的修复方案
- 统一所有JUnit 5相关依赖的版本:把所有JUnit平台、JUnit Jupiter、JUnit Vintage的版本统一升级到同一个稳定版本(尽量选较新的稳定版,兼容性更好)。比如可以改成这样:
// 升级JUnit 4到兼容的新版本 testImplementation 'junit:junit:4.13.2' // Spek相关依赖保留1.1.5(和JUnit Platform 1.x兼容) testImplementation 'org.jetbrains.spek:spek-api:1.1.5' testImplementation 'org.jetbrains.spek:spek-junit-platform-engine:1.1.5' // 统一JUnit Platform版本到1.8.2(和JUnit Jupiter 5.8.2兼容) testImplementation 'org.junit.platform:junit-platform-launcher:1.8.2' testImplementation 'org.junit.platform:junit-platform-runner:1.8.2' // 统一JUnit Vintage和Jupiter版本 testImplementation 'org.junit.vintage:junit-vintage-engine:5.8.2' testImplementation 'org.junit.jupiter:junit-jupiter-api:5.8.2' testImplementation 'org.junit.jupiter:junit-jupiter-params:5.8.2' testImplementation 'org.junit.jupiter:junit-jupiter-engine:5.8.2' - 更新IDE的JUnit插件:打开IntelliJ IDEA的
File > Settings > Plugins,找到JUnit相关插件,确保它是最新版本——旧插件可能和新版本的JUnit依赖不兼容。 - 清理项目缓存并重新构建:在终端执行
./gradlew clean build(Windows系统用gradlew.bat clean build),彻底清理旧的编译文件和依赖缓存,然后重新构建项目,避免缓存的旧依赖干扰。 - 精简不必要的依赖:如果你不需要同时支持JUnit 4和JUnit 5,可以删掉冗余的依赖。比如只使用JUnit 5的话,可以去掉
junit:junit:4.13.2和org.junit.vintage:junit-vintage-engine;如果后续考虑升级Spek,也可以换成Spek 2.x版本(它对新的JUnit Platform支持更好)。
内容的提问来源于stack exchange,提问作者kokilayaa
相关产品推荐
相关产品推荐

