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

前端 未结 30 2292
盖世英雄少女心
盖世英雄少女心 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:23

    As everyone informed it's IDE bug, I tried in Eclipse and STS. In both the cases, it is failing.

    As a workaround, I have fixed by modifying the pom.xml file like below.

    I have added these two maven dependencies junit-jupiter-engine and junit-platform-launcher.

    pom.xml

    <!-- https://mvnrepository.com/artifact/org.junit.jupiter/junit-jupiter-engine -->
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-engine</artifactId>
            <version>${junit-jupiter.version}</version>
            <scope>test</scope>
        </dependency>
    
        <!-- https://mvnrepository.com/artifact/org.junit.platform/junit-platform launcher -->
        <dependency>
            <groupId>org.junit.platform</groupId>
            <artifactId>junit-platform-launcher</artifactId>
            <version>${junit-platform.version}</version>
            <scope>test</scope>
        </dependency>
    </dependencies>
    

    Also please make sure to add the version of both the maven dependencies in the properties tag.

    <properties>
        <java.version>1.8</java.version>
        <junit-jupiter.version>5.2.0</junit-jupiter.version>
        <junit-platform.version>1.2.0</junit-platform.version>
    </properties>
    
    0 讨论(0)
  • 2020-11-29 23:25

    I used actually spring-tool-suite-4-4.5.1 and I had this bug when I want run a test class. and the solution was to add to 'java build path', 'junit5' in Libraries java build path img

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

    FYI, another cause of "No tests found using junit5" is (inadvertently or intentionally) declaring the test cases "private":

    // Example of test case that doesn't get included
    @Test
    private void testSomeMethod() {
    }
    

    They need to be public.

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

    I ran into the same error, and in my case it was a simple matter of going to Project Properties > Maven > Update project and/or cleaning and rebuilding the project.

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

    You ran into Eclipse bug 525948 which has already been fixed and which will be published in the upcoming release Oxygen.3 (4.7.3), March 21, 2018.

    As workaround, put your test code in a separate project and add the project under test to the modulepath, but do not add a module-info.java to your test project. With your project, class and module naming, it should look something like this:

    See also my video that shows Java 9 and JUnit 5 in Eclipse Oxygen.1a in action

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

    I faced this issue with Eclipse (Version: 2019-12 (4.14.0)) too. The solution seems either to use IntelliJ or to use the Maven test to run such tests in Eclipse.

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