ChatGPT解决这个技术问题 Extra ChatGPT

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

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.

The description

Under the run configuration, eclipse can't even find the method which is annotated with @Test, but instead only shows me "(all methods)". The following picture hopefully gives a better glimps of my setup:

https://i.stack.imgur.com/wVTPt.png

Console output:

java.lang.NoClassDefFoundError: org/junit/platform/launcher/core/LauncherFactory
    at org.eclipse.jdt.internal.junit5.runner.JUnit5TestLoader.<init>(JUnit5TestLoader.java:31)
    at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
    at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
    at java.base/java.lang.reflect.Constructor.newInstance(Unknown Source)
    at java.base/java.lang.Class.newInstance(Unknown Source)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.createRawTestLoader(RemoteTestRunner.java:368)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.createLoader(RemoteTestRunner.java:363)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.defaultInit(RemoteTestRunner.java:307)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.init(RemoteTestRunner.java:222)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:206)
Caused by: java.lang.ClassNotFoundException: org.junit.platform.launcher.core.LauncherFactory
    at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(Unknown Source)
    at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(Unknown Source)
    at java.base/java.lang.ClassLoader.loadClass(Unknown Source)
    ... 11 more

What I tried so far

I've already tried

to remove the test folder from build path and add it again.

to start the test with hovering over the method annotated with @Test and then click "Run as JUnit Test".

remove JUnit from Buildpath and add it again

restart eclipse

I've also moved the project whole project from one machine to another machine and tried it with the provided eclipse installation there

to rename the test method.

to retype the @Test annotation

Some of these steps can be found here, but in the end the problem remained.

But did you try to find out the difference as stated in the answer to this -stackoverflow.com/questions/34413/…?

J
JonathanDavidArndt

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:

https://i.stack.imgur.com/oNPRd.png

I ran the test again and it worked.


It's funny that selecting "JUnit4" in run configurations while using JUnit5. I won't call that a good solution that I can rely on while I'm dealing with a big project.
I also find this solution suspicious, it even lacks an explanation as to why this works and what risks one may take.
If doing this didn't solve the issue for you then go to classpath tab on the same window and add JUnit to the Bootstrap Libraries.
Thanks; selecting JUnit 4 resolved my issues. For now, it's the simplest way to resolve the issue.
In my opinion this less invasive solution, that is, without modifying the source code.
A
Andrii Karaivanskyi

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.


If using gradle usually it goes to build.gradle. If you are using maven it's pom.xml. The format for maven is different though.
That fixed it for me. Thanks!
Here's the pom.xml snippet to go inside : org.junit.jupiter junit-jupiter-engine 5.1.0 test org.junit.platform junit-platform-launcher 1.1.0 test
Still adding junit-platform 1.1.0 did not work for me. It shows same message "No tests found with test runner using JUnit 5" "
@PSatishPatro After adding the above. You need to go to run configuration for that Junit test case and then go to Classpath tab-> Advanced-> Add Library-> OK-> Junit-> Finish. Then go ahead and Run it.This will add the library during the execution of the Junit
a
armin.miedl

In my case, the problem was myself and no IDE like Eclipse. I've imported the JUnit 4 Test class.

So do NOT import this one:

import org.junit.Test  // JUnit 4

But DO import that one:

import org.junit.jupiter.api.Test // JUnit 5

I was switching between JUnit 4 and 5 because the current project is using JUnit 5, but when reporting bugs to Hibernate etc. I need to switch to JUnit 4. I ended up accidentally mixing the imports. 🤦‍♂️
This answer needs more upvotes!
J
Jerry Carney

SIMPLE FIX: (Add JUnit 5 Library)

INSTRUCTIONS:

Right click on project -> Build Path -> Configure Build Path

In the pop-up -> Add Library -> JUnit -> JUnit 5 -> Finish -> Apply

You should see the JUnit 5 Library (and its jars) added to your project

Right click on project -> Maven -> Update Project -> OK


GRADLE FIX: (Add JUnit 5 Library) INSTRUCTIONS: * Right click on project -> Build Path -> Configure Build Path * In the pop-up -> Add Library -> JUnit -> JUnit 5 -> Finish -> Apply * You should see the JUnit 5 Library (and its jars) added to your project * Right click on project -> Gradle -> Refresh Gradle Project * Right click on project -> Run As -> Junit Test
This should be accepted answer, thank you @JerryCarney
h
howlger

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:

https://i.stack.imgur.com/NBXME.png

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


@nullpointer I mean to create a new Java project that is not modular (has no module-info.java). In Eclipse a Java project has a single output folder and therefor it can only contain one or none module-info.java. But you can add modules to the project's modulepath even though the project is not modular (has no module-info.java).
@ShadowDragon the drawback with this hack is that we need a different package name for the test classes, thus cant do package-private testing
I just installed Oxygen.3 (4.7.3) and the problem persists.
@chomp If you have 2018-09, it's a different issue even if the symptoms look similar. If no tests were found, this is mostly due to an incorrect classpath/module path (Project > Properties: Java Build Path).
@howlger Thanks. After upgrading to latest version this problem gone. Thanks again for your help :).
D
Developer Guy

Answers so far did not adress the problem of sharing code with other people who don't necessarily use Eclipse. Here is one proposition. The key is to use a maven profile to solve the Eclipse Case.

It assumes you have defined a property junit5.version in your pom like:

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <junit5.version>5.1.1</junit5.version>
</properties>

then in the profiles section add the following:

<profiles>
    <profile>
        <id>eclipse</id>
        <dependencies>
            <dependency>
                <groupId>org.junit.jupiter</groupId>
                <artifactId>junit-jupiter-engine</artifactId>
            </dependency>
            <dependency>
                <groupId>org.junit.platform</groupId>
                <artifactId>junit-platform-launcher</artifactId>
            </dependency>
        </dependencies>
        <dependencyManagement>
            <dependencies>
                <dependency>
                    <groupId>org.junit.jupiter</groupId>
                    <artifactId>junit-jupiter-engine</artifactId>
                    <version>${junit5.version}</version>
                    <scope>test</scope>
                </dependency>
                <dependency>
                    <groupId>org.junit.platform</groupId>
                    <artifactId>junit-platform-launcher</artifactId>
                    <version>1.1.1</version>
                    <scope>test</scope>
                </dependency>
            </dependencies>
        </dependencyManagement>
    </profile>
</profiles>

All you have to do after this is to select the profile in your local Eclipse: Right click on your project and select Maven > Select Maven Profiles... (or hit Ctrl + Alt + P), and then check the "eclipse" profile we just created.

https://i.stack.imgur.com/9PFFU.png

With that you are done. Your Eclipse will run Junit 5 tests as expected, but the configuration you added won't pollute other builds or other IDE


h
hbhattac

Adding this maven dependency with JUnit Jupiter (v.5.5.1) solves the issue.

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

I had a JAR project created for Spring Boot 2.x which brought in JUnit 5. I added this dependency and it appears to have fixed it. Thanks!
This solutions fixed my issue in STS IDE as well.
M
Mike B

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.


T
T3rm1

None of the solutions helped:

The problem is that Eclipse 2018-12 has support for JUnit 5.3.1. If you start it with a version before that, it will fail.

So make sure you use at least 5.3.1.

5.4.0 does work too.

In case your parent pom is Spring Boot, you need to make sure (in dependency management) that junit-jupiter-api is set to the same version. You don't need junit-platform-runner or -launcher!


Thanks for the tip. I am using 2019-3 and I added property <junit-jupiter.version>5.4.1</junit-jupiter.version> to override the version used in Spring boot (2.1.3.RELEASE).
This was the one that fixed it for me. I removed the -launcher dependency and it started working. (eclipse 2019-09 and junit 5.4.2)
This solution works for me as well. However, in my case, I changed from 5.3.1 to 5.5.1 and the junit-platform-engine to 1.5.1.
This fixed it for me. In eclipse 2020-03 tests did not start with junit version 5.5.2 but with 5.6.0. As a general update for your answer: It might be necessary to use at least the junit version eclipse uses.
G
Gerold Broser

Following Andrii Karaivanskyi's answer here's the Maven POM declaration:

<properties>
    ...
    <junit-jupiter.version>5.2.0</junit-jupiter.version>
    <junit-platform.version>1.2.0</junit-platform.version>
    ...
</properties>

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

UPDATE

As per comment by Alexander Wessel you can use org.junit:junit-bom as described in his answer to question Eclipse No tests found using JUnit 5 caused by NoClassDefFoundError for LauncherFactory.


Don't declare properties for the jupiter and platform versions, use the BOM instead. Basically add a dependency on the BOM in the dependencyManagement tag with scope=import type=pom, then add dependencies to junit-platform-launcher and junit-jupiter-engine in test scope, without giving a version in the dependencies section. The BOM will always ensure that you have only one version of JUnit Platform and Jupiter on classpath, and that the respective versions are the ones intended (e. g. 5.2.0 with 1.2.0).
@AlexanderWessel I have no BOM. This is just an example excerpt which dependencies one has to declare with Maven, wherever this may be.
it's not wether you have (or rather your project has) a BOM itself. I was explaining that by importing the BOM that JUnit 5 provides, you can ensure the Junit Jupiter and Junit Platform versions are compatible, and you won't need to declare a version in the individual dependencies, thus also no longer needing to declare the version properties. To be clear: Your example does work fine. But importing the JUnit BOM is a bit more elegant (in my humble opinion) and, in some circumstances, safer.
f
fatima fahmi

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

https://i.stack.imgur.com/4Vnjm.png


I used this solution at first and it worked, then I've read the other answers and I've found that the "correct" one was @armin.miedl's. I removed the JUnit 5 library from the build path and replaced the Test annotation import with the correct one.
A
Alok Ranjan

I also faced the same issue you just need to add the library , Junit Library is already provided along with Eclipse so you just need to follow below

Build Path > Configure Build Path > library > Add library > JUnit > Next > finish

It works for me


It worked fine in eclipse. Version: 2020-06 (4.16.0) Build id: 20200615-1200
I
Ion Timotin

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

Press right click to project folder and choose Properties OR select project folder and press combination cmd + i. select from list "Java Build Path". select "Libraries" tab If JUnit 5(or JUnit 4) is added to "Modulepath", select the "JUnit 5" and press Remove. select "Classpath", press "Add Library...". from opened "Add Library" window, select JUnit, press next. Select JUnit library version that you need and press Finish.

That is all. Try to run test again.


a
atline

replace:

import org.junit.Test;

with:

import org.junit.jupiter.api.Test;

d
du-it

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.


Both class and method required public keyword. junit.org/junit4/javadoc/4.12/org/junit/Test.html
A
Arvind Katte

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>

Adding the junit-platform-launcher dependency solved the problem for me. Thanks
Y
Y.halaoui

you should know that :

@Before from junit4 goes with @Test : "import org.junit.Test"

AND

@BeforeEach from Junit5 goes with : "import org.junit.jupiter.api.Test"

so make sure you are using the imports from the same version of Junit , otherwise it w'ont Work I guess.


B
Branislav Osadkovski

@Test annotation must be imported from org.junit.jupiter.api.Test so the Junit5 can read it. Junit4 use @Test annotations imported from org.junit.Test package.


m
mahe

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


Why did you add "org.junit.vintage" if you are using JUnit5? It is only for backward compatibility (like using old JUnit) AFAIK.
Because with that project and STS 3.9.1 I could not/did not use JUnit5 but JUnit4.
L
Lump

I am using eclipse 2019-09.

I had to update the junit-bom version to at least 5.4.0. I previously had 5.3.1 and that caused the same symptoms of the OP.

My config is now:

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.junit</groupId>
                <artifactId>junit-bom</artifactId>
                <version>5.5.2</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

another option is to add 5.4.0 to pom properties
R
Ravi Ranjan

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>

G
Gerold Broser

Since it's not possible to post code blocks into comments here's the POM template I am using in projects requiring JUnit 5. This allows to build and "Run as JUnit Test" in Eclipse and building the project with plain Maven.

<project
    xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>group</groupId>
    <artifactId>project</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>project name</name>

    <dependencyManagement>
      <dependencies>
        <dependency>
        <groupId>org.junit</groupId>
        <artifactId>junit-bom</artifactId>
        <version>5.3.1</version>
        <type>pom</type>
        <scope>import</scope>
        </dependency>
      </dependencies>
    </dependencyManagement>

    <dependencies>
      <dependency>
        <groupId>org.junit.jupiter</groupId>
        <artifactId>junit-jupiter-engine</artifactId>
        <scope>test</scope>
      </dependency>

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

      <dependency>
        <!-- only required when using parameterized tests -->
        <groupId>org.junit.jupiter</groupId>
        <artifactId>junit-jupiter-params</artifactId>
        <scope>test</scope>
      </dependency>
    </dependencies>
</project>

You can see that now you only have to update the version in one place if you want to update JUnit. Also the platform version number does not need to appear (in a compatible version) anywhere in your POM, it's automatically managed via the junit-bom import.


R
RobC

You can use only junit-jupiter as a test dependency instead of junit-jupiter-api, junit-platform-launcher, junit-jupiter-engine.

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

This solution worked for me with JUnit 5.6.2, Java 11 (and eclipse 2020-03/4.15.0)
K
KBusc

I'm using:

Spring Tool Suite 4 Version: 4.4.2.RELEASE Build Id: 201911201053 Java version: 1.8.0_191

and the message displayed is No tests found with test runner 'JUnit5'

what worked for me was the configuration below

    <properties>
        <java.version>1.8</java.version>
        <junit-jupiter.version>5.5.2</junit-jupiter.version>
        <junit-platform.version>1.5.2</junit-platform.version>
    </properties> 

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

T
Tenusha Guruge

You might be importing @Test from org.junit.Test, which is a JUnit 4 annotation. The Junit5 test runner will not discover it.

The Junit5 test runner will discover a test annotated with org.junit.jupiter.api.Test

Found the answer from Import org.junit.Test throws error as "No Test found with Test Runner "JUnit 5""


S
Steephen

You should make sure your test case function is public rather than private to make it accessible by Test Runner.

@Test
private void testmethod(){}

to

@Test
public void testmethod(){}

H
Harpreet Singh

Same error i faced in eclipse version Oxygen.3a Release (4.7.3a) . There is issue in Maven Dependencies mismatch.To solve i have updated my Pom.xml with following dependecies.

http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4.0.0 com.netapp.junitnmactiopractice JunitAndMactioPractice 0.0.1-SNAPSHOT

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <java.version>1.8</java.version>
    <junit.jupiter.version>5.1.1</junit.jupiter.version>
    <junit.platform.version>1.1.1</junit.platform.version>
</properties>

<build>
    <plugins>
        <plugin>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.1</version>
            <configuration>
                <source>${java.version}</source>
                <target>${java.version}</target>
            </configuration>
        </plugin>
    </plugins>
</build>

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


Have you seen the JUnit 5 User Guide regarding junit-platform-runner: "Runner for executing tests and test suites on the JUnit Platform in a JUnit 4 environment."
A
Aegon

you should change

@Test
public static void testmethod(){}

to

@Test
public void testmethod(){}

the @Test is unsupport static method


D
Dineshotham Kumar Khambhammett

For me, I configured the build path to add JUnit 5 Library and also by adding the dependency

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

seperately.


S
Srini Ramjee

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.


关注公众号,不定期副业成功案例分享
Follow WeChat

Success story sharing

Want to stay one step ahead of the latest teleworks?

Subscribe Now