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

236,703

Solution 1

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:

enter image description here

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

Solution 2

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.

Solution 3

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.

Solution 4

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

Solution 5

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
Share:
236,703
ShadowDragon
Author by

ShadowDragon

I'm always learning new languages... Newest: C++

Updated on December 04, 2021

Comments

  • ShadowDragon
    ShadowDragon over 2 years

    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.

  • Naman
    Naman over 6 years
    Put your test code in a separate project do you mean defining another module for tests?
  • howlger
    howlger over 6 years
    @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).
  • Naman
    Naman over 6 years
    Not very clear from the answer. Could you share a sample structure/screenshot for the suggestion?
  • howlger
    howlger over 6 years
    @nullpointer I need to correct myself: in Eclipse a Java project can have multiple output folders (one output folder per source folder). But this can not be used (at least I don't know how) for a workaround of this Eclipse bug (see my edited answer).
  • Naman
    Naman over 6 years
    Okay. I would wait for the OP to confirm that this works. Also would love to see a sample if you could update in the answer for the workaround suggested. IMO, would add value to the answer for people looking for that alternate until the bug fix as pointed by you. :)
  • ShadowDragon
    ShadowDragon over 6 years
    This workaround works for me. ;) Just had to figure out that eclipse added my data to the lib module path and not project module or classpath. Also, the link in the answer to the video doesn't work. Would be nice if this can be fixed.
  • howlger
    howlger over 6 years
    @ShadowDragon Sorry for the broken link which should work now.
  • kleopatra
    kleopatra over 6 years
    @ShadowDragon the drawback with this hack is that we need a different package name for the test classes, thus cant do package-private testing
  • jvdh
    jvdh about 6 years
    in which file would I add the dependency?
  • Andrii Karaivanskyi
    Andrii Karaivanskyi about 6 years
    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.
  • Chris
    Chris about 6 years
    That fixed it for me. Thanks!
  • user118967
    user118967 about 6 years
    I just installed Oxygen.3 (4.7.3) and the problem persists.
  • user118967
    user118967 about 6 years
    Here's the pom.xml snippet to go inside <dependencies>: <dependency> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter-engine</artifactId> <version>5.1.0</version> <scope>test</scope> </dependency> <dependency> <groupId>org.junit.platform</groupId> <artifactId>junit-platform-launcher</artifactId> <version>1.1.0</version> <scope>test</scope> </dependency>
  • howlger
    howlger about 6 years
    @user118967 I tested it with Oxygen.3 and it works for me, as you can watch here: youtube.com/watch?v=uJNzMGrbXbI&t=42s Do you get the same problem with Photon M6? Which error message do you get exactly?
  • user118967
    user118967 about 6 years
    I kept getting the same error as initially (same as the OP). However, I used Andrii's solution and it worked. Thanks for replying!
  • Gerold Broser
    Gerold Broser almost 6 years
    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."
  • Gerold Broser
    Gerold Broser almost 6 years
    This answer should be the accepted, rather than the one suggesting a separate test project!
  • Levent Divilioglu
    Levent Divilioglu over 5 years
    Why did you add "org.junit.vintage" if you are using JUnit5? It is only for backward compatibility (like using old JUnit) AFAIK.
  • Levent Divilioglu
    Levent Divilioglu over 5 years
    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.
  • mahe
    mahe over 5 years
    Because with that project and STS 3.9.1 I could not/did not use JUnit5 but JUnit4.
  • Alexander Wessel
    Alexander Wessel over 5 years
    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).
  • Gerold Broser
    Gerold Broser over 5 years
    @AlexanderWessel I have no BOM. This is just an example excerpt which dependencies one has to declare with Maven, wherever this may be.
  • Alexander Wessel
    Alexander Wessel over 5 years
    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.
  • Alexander Wessel
    Alexander Wessel over 5 years
  • howlger
    howlger over 5 years
    @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).
  • Venkatesh Manohar
    Venkatesh Manohar about 5 years
    Adding the junit-platform-launcher dependency solved the problem for me. Thanks
  • Ritesh
    Ritesh about 5 years
    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).
  • Raymond
    Raymond about 5 years
    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)
  • Amadán
    Amadán almost 5 years
    I also find this solution suspicious, it even lacks an explanation as to why this works and what risks one may take.
  • Arwan Khoiruddin
    Arwan Khoiruddin almost 5 years
    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.
  • Satish Patro
    Satish Patro over 4 years
    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" "
  • hmehandi
    hmehandi over 4 years
    Both class and method required public keyword. junit.org/junit4/javadoc/4.12/org/junit/Test.html
  • Uri Loya
    Uri Loya over 4 years
    another option is to add <junit-jupiter.version>5.4.0</junit-jupiter.version> to pom properties
  • Jaison Varghese
    Jaison Varghese over 4 years
    @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
  • Abhi
    Abhi over 4 years
    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.
  • Ara Kokeba
    Ara Kokeba over 4 years
    I had this issue with STS. My workaround was simply to remove the test class and then recreate it.
  • Datta
    Datta over 4 years
    @howlger you said it is fixed in upcoming release Oxygen.3 (4.7.3), March 21, 2018. I have eclipse version as "Version: Oxygen.3a Release (4.7.3a) Build id: 20180405-1200" but still I face same error. I am using Java 8. Also as suggested below by few programmers, when I "Run Configurations' and changing the "Test runner:" selection to 'JUnit 4' it works. But I don't want to do that. I want to run my Junit with Junit 5 only. Can anyone please suggest me correct solution. Thanks in advance.
  • howlger
    howlger over 4 years
    @Datta You face a different issue. You are more than 2 years and 7 releases behind. The current Eclipse version is 2019-12 (4.14). Do not wast time by using outdated software.
  • Kawu
    Kawu over 4 years
    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. 🤦‍♂️
  • Datta
    Datta over 4 years
    @howlger Thanks. After upgrading to latest version this problem gone. Thanks again for your help :).
  • pmartin8
    pmartin8 over 4 years
    See answer from StringVector for the real solution.
  • howlger
    howlger over 4 years
    @pmartin8 My answer is verified as correct (see bug report and comments above) and is specific to Eclipse Oxygen.1a (4.7.1a) and Oxygen.2 (4.7.2) only. In contrast, the answer you mentioned refers to a different issue with the same or similar error message.
  • Lars Briem
    Lars Briem about 4 years
    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.
  • Tes
    Tes about 4 years
    Thanks; selecting JUnit 4 resolved my issues. For now, it's the simplest way to resolve the issue.
  • coseos
    coseos about 4 years
    This solution worked for me with JUnit 5.6.2, Java 11 (and eclipse 2020-03/4.15.0)
  • OJVM
    OJVM almost 4 years
    It worked fine in eclipse. Version: 2020-06 (4.16.0) Build id: 20200615-1200
  • Rafael Rocha
    Rafael Rocha almost 4 years
    In my opinion this less invasive solution, that is, without modifying the source code.
  • David Bradley
    David Bradley almost 4 years
    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!
  • Jack D Menendez
    Jack D Menendez over 3 years
    This worked for me. My other projects ran tests fine but a new one did not. Exactly the same POMs with expected differences. Where are these run configurations stored anyway? Not in any files I could find.
  • Larry Ricker
    Larry Ricker over 3 years
    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
  • Vivek Ravi
    Vivek Ravi about 3 years
    This solutions fixed my issue in STS IDE as well.
  • pixel
    pixel about 3 years
    This should be accepted answer, thank you @JerryCarney
  • nonzaprej
    nonzaprej almost 3 years
    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.
  • Joel Aelwyn
    Joel Aelwyn almost 3 years
    At a guess, this may only work if you follow the convention of naming test methods "test<something>" — in which case it would be detected by the JUnit4 running as a fallback to the JUnit3 convention, which predated the availability of annotations (and thus had no @Test).
  • Pino
    Pino over 2 years
    I got that popup (without stack trace) when I had both JUnit 4 and JUnit 5 in the classpath (during a spring-boot upgrade). My tests were developed with JUnit 4 but Eclipse selected JUnit 5. The solution was to remove JUnit 5 from the classpath and to delete the old JUnit Run Configurations.
  • Monish Sen
    Monish Sen about 2 years
    This answer needs more upvotes!