Eclipse No tests found using JUnit 5 caused by NoClassDefFoundError for LauncherFactory

前端 未结 30 2293
盖世英雄少女心
盖世英雄少女心 2020-11-29 22:31

The problem

Whenever I run my projects JUnit test (using JUnit 5 with Java 9 and Eclipse Oxygen 1.a) I encounter the problem that eclipse can\'t find any tests.

相关标签:
30条回答
  • 2020-11-29 23:13

    Using STS 3.9.1 I got the same problem. However, currently I do not require any new JUnit5 features, so I tried to force using an older version. If using maven, you can add following dependencies to your pom.xml:

    <dependency>
        <groupId>org.junit.platform</groupId>
        <artifactId>junit-platform-launcher</artifactId>
        <version>${junit.platform.version}</version>
        <scope>test</scope>
    </dependency>
    
    <dependency>
        <groupId>org.junit.jupiter</groupId>
        <artifactId>junit-jupiter-engine</artifactId>
        <version>${junit.jupiter.version}</version>
        <scope>test</scope>
    </dependency>
    
    <dependency>
        <groupId>org.junit.vintage</groupId>
        <artifactId>junit-vintage-engine</artifactId>
        <version>${junit.vintage.version}</version>
        <scope>test</scope>
    </dependency>
    

    This did the trick for me (at least as long as I do not need JUnit5 explicitly).

    0 讨论(0)
  • 2020-11-29 23:13

    I got the same problem after creating a new TestCase: Eclipse -> New -> JUnit Test Case. It creates a class without access level modifier. I could solve the problem by just putting a public before the class keyword.

    0 讨论(0)
  • 2020-11-29 23:14

    From the start the error message tell you that class is not found : NoClassDefFoundError that mean the PATH to junit is the problem.

    1. Press right click to project folder and choose Properties OR select project folder and press combination cmd + i.

    2. select from list "Java Build Path".

    3. select "Libraries" tab
    4. If JUnit 5(or JUnit 4) is added to "Modulepath", select the "JUnit 5" and press Remove.
    5. select "Classpath", press "Add Library...".
    6. from opened "Add Library" window, select JUnit, press next.
    7. Select JUnit library version that you need and press Finish.

    That is all. Try to run test again.

    0 讨论(0)
  • 2020-11-29 23:14

    You are missing JUnit 5 platform launcher with group: 'org.junit.platform', name: 'junit-platform-launcher'

    Just add in ur POM:

    <dependency>
           <groupId>org.junit.platform</groupId>
           <artifactId>junit-platform-launcher</artifactId>
        </dependency>
    
    0 讨论(0)
  • 2020-11-29 23:16

    I have the same issue with STS 3.9.1. It seems like an Eclipse bug, however, to fix this you can add a test dependency junit-platform-launcher to your project (https://mvnrepository.com/artifact/org.junit.platform/junit-platform-launcher)

    This is how I did for my project which uses gradle:

    dependencies {
        // other stuff here
    
        testCompile group: 'org.junit.jupiter', name: 'junit-jupiter-engine', version: "5.${junit5MinorVersion}"
        testCompile group: 'org.junit.platform', name: 'junit-platform-launcher', version: "1.${junit5MinorVersion}"
    }
    

    gradle.properties file:

    junit5MinorVersion=1.0
    

    I believe the same applies if you see this exception while using IntelliJ IDEA.

    0 讨论(0)
  • 2020-11-29 23:17

    I fixed the issue by right clicking the test and selecting 'Run Configurations' and changing the "Test runner:" selection to 'JUnit 4' as shown here:

    I ran the test again and it worked.

    0 讨论(0)
提交回复
热议问题