java.lang.NoSuchMethodError: org.junit.platform.launcher.Launcher.execute

12,361

Solution 1

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.

Solution 2

Upgrade junit-platform-launcher version from 1.0.0 to 1.4.x may still be needed in order to be able to run your junit5 tests:

<dependency>
    <groupId>org.junit.platform</groupId>
    <artifactId>junit-platform-launcher</artifactId>
    <version>1.4.2</version>
    <scope>test</scope>
</dependency>

I've experienced a very similar problem in eclipse, and found the solution here

Hope this helps !

Solution 3

I was experiencing something similar in VSCode, and thought I'd share my result.

I was using a mis-mash of testing dependencies in my pom.xml, which I saw by doing mvn dependency:tree. Removing Specific junit-jupiter dependencies and simply using org.junit.jupiter:junit-jupiter made everything work again (both the Test Execution in VSCode, and mvn test on the command line).

Ultimately the only Jupiter dependencies I have in my pom.xml now is:

<dependency>
    <groupId>org.junit.jupiter</groupId>
    <artifactId>junit-jupiter</artifactId>
    <version>5.6.0</version>
    <scope>test</scope>
</dependency>

Source: https://github.com/junit-team/junit5/issues/1773

Share:
12,361
kokilayaa
Author by

kokilayaa

Updated on June 15, 2022

Comments

  • kokilayaa
    kokilayaa almost 2 years

    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?

    • Devesh Chanchlani
      Devesh Chanchlani almost 6 years
      I do not see any issue in this, could you share a bit more.
  • Árpád Magosányi
    Árpád Magosányi almost 5 years
    Well, I use eclipse, and I have encountered the problem.
  • rexxar
    rexxar almost 4 years
    @ÁrpádMagosányi I believe it should be a similar process with Eclipse if you're using Maven. I'd say go to the Eclipse's equivalent of the dependencies folder and get the versions there. I don't have Eclipse so i don't know the exact procedure, but compare my, Christian Nuss's and hd84335's solution, maybe that will help you.
  • Árpád Magosányi
    Árpád Magosányi almost 4 years
    As I remember the solution for me was the one @hd84335 has given.