java.lang.NoSuchMethodError:org.junit.platform.launcher.Launcher.execute [英] java.lang.NoSuchMethodError: org.junit.platform.launcher.Launcher.execute

查看:278
本文介绍了java.lang.NoSuchMethodError:org.junit.platform.launcher.Launcher.execute的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试运行以下示例单元测试用例

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'

对此有什么解决办法吗?

解决方案

TL; DR 根据最初在IDEA_INSTALLATION_HOME/plugins/junit/lib pom.xml中的依赖项降级. >


长版:

假设您使用的是Intellij IDEA的版本早于2017.3;那么您就可以将这些选择作为对另一个SO问题的正式回答: https: //intellij-support.jetbrains.com/hc/en-us/community/posts/115000791190-Intellij-does-not-run-Junit5-tests

将其粘贴到此处以使其更加可见:

IDE对旧的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软件包的Test类.在此之前,在尝试运行测试时,我会不断得到NoSuchMethodError.

I'm trying to run the following example unit test case

class ExampleUnitTest {

    @Test
    fun addition_is_Correct() {
        assertEquals(4, (2 + 2).toLong())
    }

}

but I get the following exception

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)

even though I have updated all the Junit dependencies build.gradle file like given below

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'

is there any solution for this?

解决方案

TL;DR downgrade your dependencies in pom.xml according to versions that originally came with IDEA found in IDEA_INSTALLATION_HOME/plugins/junit/lib


Longer version:

Let's presume you're using a version of the Intellij IDEA older than 2017.3; then you have these choices that were given as an official answer to another SO question: https://intellij-support.jetbrains.com/hc/en-us/community/posts/115000791190-Intellij-does-not-run-Junit5-tests

Pasting it here to to make it more visible:

IDE has compilation dependency on the old junit 5 launcher jar and it is not compatible with current released version. So you have a choice to update IDE so it will be compatible with the junit version you use or to downgrade the junit version (check what version was bundled in IDEA_INSTALLATION_HOME/plugins/junit/lib). 2017.1 had only experimental support for junit 5 as junit 5 was not released yet at that time. Sorry for the inconvenience.

So, go to your IDEA_INSTALLATION_HOME/plugins/junit/lib folder and check the versions in the names of the jar files found there. Should be something like this:

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

Now use the junit- filename's version suffix in your module's pom.xml properties setup:

<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>

I can confirm that after changing to older versions, I could run Test classes that were using org.junit.jupiter package. Before this I was constantly getting the NoSuchMethodError when trying to run the Tests.

这篇关于java.lang.NoSuchMethodError:org.junit.platform.launcher.Launcher.execute的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆