[java] 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://imgur.com/a/FTe9c

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.

This question is related to java eclipse junit java-9 junit5

The answer is


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!


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(){}

Add in your POM:

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

Converting the Java project to Maven Project fixed the issue for me. The conversion was done on Eclipse by: Right Click Project -> Configure -> Convert to Maven Project


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>

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.


When I change my jupiter api version into latest one, it was solved. Meanwhile, my eclipse and other eclipse extensions ide's (such as STS) is getting build path error. For every maven update, ide forces me to set the JRE System Library.


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


Removing the JUnit library, and then adding it again fixed this issue for me.


I got the same problem in a SpringBoot project and, in my case, the cause was that the ouput folder '/target/test-classes' was empty.

Fixed it manually in the .classpath file, looking for the classpathentry with the attribute "test" and changing the content of the output attribute:

From this:

<classpathentry kind="src" output="<project folder>/target/test-classes" path="src/test/java">
    <attributes>
        <attribute name="test" value="true"/>
        <attribute name="optional" value="true"/>
        <attribute name="maven.pomderived" value="true"/>
    </attributes>
</classpathentry>

to this:

<classpathentry kind="src" output="target/test-classes" path="src/test/java">
    <attributes>
        <attribute name="test" value="true"/>
        <attribute name="optional" value="true"/>
        <attribute name="maven.pomderived" value="true"/>
    </attributes>
</classpathentry>

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.

Selection Of Maven Profile

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


you should change

@Test
public static void testmethod(){}

to

@Test
public void testmethod(){}

the @Test is unsupport static method


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.


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

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>

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:

Screenshot of run configruations

I ran the test again and it worked.


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.


It may cause by the version of junit-platform-launcher / junit-platform-runner.

1.1.0 does not work

1.4.1 works in my setup.

I think this is a bug in eclipse as it is working with higher version libraries of junit and not other version.

This resolved my issue. The other resolution looked less feasible or risky to me. Thanks.


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>


Just added my Test.class via eclipse menu and worked. Right click to project -> New -> Other -> JUnit -> JUnit Test Case


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.


adding org.junit.platform to your pom and build it. Next, you need to go to "Run Configuration" of your TEST FILE and add JUNIT in the classpath, APPLY->RUN will resolve the issue.


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.


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


Try to reconfigure your unit test configurations to be the project JRE. enter image description here


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>

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

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


I had a similar issue. It was due to an incorrect source path in properties. Open project properties -> Java Build Path -> Sources. And remove the incorrect path if it is your case


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>

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.


replace:

import org.junit.Test;

with:

import org.junit.jupiter.api.Test;

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.


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>

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.


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


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>

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.


I copied some code and created a TC. Basically converted a normal java class to JUnit TC. I was gettgig error. Later I created a new JUnit Class from New -> Junit TC. Copied the same code. It works fine and error vanished.


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.


This issue happening while implementing Mockito with spring boot api. Below add below dependency in in gradle file

compile group: 'org.mockito', name: 'mockito-core', version: '2.22.0'

Note: Spring boot version 2.2.0


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

enter image description here


Examples related to java

Under what circumstances can I call findViewById with an Options Menu / Action Bar item? How much should a function trust another function How to implement a simple scenario the OO way Two constructors How do I get some variable from another class in Java? this in equals method How to split a string in two and store it in a field How to do perspective fixing? String index out of range: 4 My eclipse won't open, i download the bundle pack it keeps saying error log

Examples related to eclipse

How do I get the command-line for an Eclipse run configuration? My eclipse won't open, i download the bundle pack it keeps saying error log strange error in my Animation Drawable How to uninstall Eclipse? How to resolve Unable to load authentication plugin 'caching_sha2_password' issue Class has been compiled by a more recent version of the Java Environment Eclipse No tests found using JUnit 5 caused by NoClassDefFoundError for LauncherFactory How to downgrade Java from 9 to 8 on a MACOS. Eclipse is not running with Java 9 "The POM for ... is missing, no dependency information available" even though it exists in Maven Repository The origin server did not find a current representation for the target resource or is not willing to disclose that one exists. on deploying to tomcat

Examples related to junit

Eclipse No tests found using JUnit 5 caused by NoClassDefFoundError for LauncherFactory How to resolve Unneccessary Stubbing exception JUnit 5: How to assert an exception is thrown? How do I mock a REST template exchange? Class Not Found: Empty Test Suite in IntelliJ Unable to find a @SpringBootConfiguration when doing a JpaTest Failed to load ApplicationContext (with annotation) Example of Mockito's argumentCaptor Mockito - NullpointerException when stubbing Method Spring jUnit Testing properties file

Examples related to java-9

what is an illegal reflective access Error occurred during initialization of boot layer FindException: Module not found Failed to run sdkmanager --list with Java 9 Eclipse No tests found using JUnit 5 caused by NoClassDefFoundError for LauncherFactory Failed to install android-sdk: "java.lang.NoClassDefFoundError: javax/xml/bind/annotation/XmlSchema" Java: How to resolve java.lang.NoClassDefFoundError: javax/xml/bind/JAXBException

Examples related to junit5

Deprecated Gradle features were used in this build, making it incompatible with Gradle 5.0 Eclipse No tests found using JUnit 5 caused by NoClassDefFoundError for LauncherFactory JUnit 5: How to assert an exception is thrown? Difference between @Before, @BeforeClass, @BeforeEach and @BeforeAll