我正在尝试运行以下示例单元测试用例
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)尽管我已经更新了所有Junit依赖项build.gradle文件,如下所示
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'对此有什么解决方案吗?
发布于 2018-06-20 01:10:08
TL;DR根据IDEA_INSTALLATION_HOME/plugins/junit/lib中最初随IDEA提供的版本,将您在pom.xml中的依赖项降级
更长的版本:
假设您使用的是Intellij IDEA的一个早于2017.3的版本;然后您有这些选项,这些选项是对另一个SO问题的官方答案:https://intellij-support.jetbrains.com/hc/en-us/community/posts/115000791190-Intellij-does-not-run-Junit5-tests
将其粘贴到此处以使其更可见:
junit集成开发环境对旧的
5启动程序jar具有编译依赖性,并且它与当前发布的版本不兼容。因此,您可以选择更新IDE以使其与您使用的junit版本兼容,也可以选择降级junit版本(检查IDEA_INSTALLATION_HOME/plugins/junit/lib中捆绑的版本)。2017.1只对junit 5提供了实验支持,因为当时junit 5还没有发布。很抱歉给您带来不便。
因此,转到您的IDEA_INSTALLATION_HOME/plugins/junit/lib文件夹并检查在那里找到的jar文件的名称中的版本。应该是这样的:
user@comp:IDEA_INSTALLATION_HOME/plugins/junit/lib]$ ls
idea-junit.jar junit-platform-runner-1.0.0-M4.jar
junit5-rt.jar junit-platform-suite-api-1.0.0-M4.jar
junit-jupiter-api-5.0.0-M4.jar junit-rt.jar
junit-jupiter-engine-5.0.0-M4.jar junit-vintage-engine-4.12.0-M4.jar
junit-platform-commons-1.0.0-M4.jar opentest4j-1.0.0-M2.jar
junit-platform-engine-1.0.0-M4.jar resources_en.jar
junit-platform-launcher-1.0.0-M4.jar现在,在模块的pom.xml properties设置中使用junit-文件名的版本后缀:
<project>
...
<properties>
<junit.jupiter.version>5.0.0-M4</junit.jupiter.version>
<junit.platform.version>1.0.0-M4</junit.platform.version>
<junit.vintage.version>4.12.0-M4</junit.vintage.version>
...
</properties>
...
</project>我可以确认,在切换到旧版本后,我可以运行使用org.junit.jupiter包的测试类。在此之前,当我试图运行测试时,我总是得到NoSuchMethodError。
发布于 2020-06-19 04:03:14
可能仍然需要将junit-platform-launcher版本从1.0.0升级到1.4.x,以便能够运行您的junit5测试:
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-launcher</artifactId>
<version>1.4.2</version>
<scope>test</scope>
</dependency>我在eclipse中遇到了一个非常类似的问题,并找到了解决方案here
希望这能有所帮助!
发布于 2020-02-19 04:13:35
我在VSCode中经历了类似的事情,我想我应该分享我的结果。
我在我的pom.xml中使用了测试依赖项的混杂,这是我执行mvn dependency:tree时看到的。删除特定的junit-jupiter依赖项并简单地使用org.junit.jupiter:junit-jupiter可以让一切工作起来(无论是VSCode中的测试执行,还是命令行上的mvn test )。
最终,我的pom.xml中唯一的Jupiter依赖项是:
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>5.6.0</version>
<scope>test</scope>
</dependency>https://stackoverflow.com/questions/50323335
复制相似问题