Skip to content

JUnit5 test are not able to run in Eclipse #1007

Closed

Description

@oconnelc

Steps to recreate:

  1. In Eclipse, select a unit test to run by right-clicking and selecting: "Run Configurations." Select a JUnit execution and ensure that the JUnit5 runner is selected:
    image

  2. Click the Run button.

  3. The following error is shown:
    image
    and the following stack trace is displayed:

java.lang.NoClassDefFoundError: org/junit/platform/commons/PreconditionViolationException
	at org.eclipse.jdt.internal.junit5.runner.JUnit5TestLoader.createUnfilteredTest(JUnit5TestLoader.java:75)
	at org.eclipse.jdt.internal.junit5.runner.JUnit5TestLoader.createTest(JUnit5TestLoader.java:66)
	at org.eclipse.jdt.internal.junit5.runner.JUnit5TestLoader.loadTests(JUnit5TestLoader.java:53)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:525)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:763)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:463)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:209)
Caused by: java.lang.ClassNotFoundException: org.junit.platform.commons.PreconditionViolationException
	at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:583)
	at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178)
	at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:521)
	... 7 more

Workaround: Run the mvn test on the command line and the tests will execute.

According to the following Stackoverflow: https://stackoverflow.com/questions/57040675/java-lang-noclassdeffounderror-org-junit-platform-commons-preconditionviolation adding the following dependency to the project will fix. (I've tried with the example project and it seems to work).

 <dependency>
            <groupId>org.junit.platform</groupId>
            <artifactId>junit-platform-commons</artifactId>
            <version>1.5.2</version>
 </dependency>

Activity

  1. lbroman commented on Oct 16, 2019

    @lbroman
    Contributor

    I got this at work today as well, and we solved it with the above suggested solution. I can prepare a PR for it if noone else already did it?

  2. iluwatar commented on Oct 16, 2019

    @iluwatar
    Owner

    Sure @lbroman, PR is welcome

  3. kaiwinter commented on Oct 17, 2019

    @kaiwinter
    Contributor

    Instead of inserting junit-platform-commons in each of the ~120 module poms a better fix would be to just update JUnit 5 to the most current version...

  4. lbroman commented on Oct 17, 2019

    @lbroman
    Contributor

    Does this fix the problem?

    I suggested in my PR (not visible here) that a better aproach might also be to upgrade the last junit 4 projects to jupiter and then pull the jupiter dependencies up to the root pom.

  5. kaiwinter commented on Oct 17, 2019

    @kaiwinter
    Contributor

    Yes it does, because JUnit 5.5.2 transitively pulls junit-platform-commons 1.5.2:

    jupiter-5 5 2

  6. lbroman commented on Oct 17, 2019

    @lbroman
    Contributor

    Tried updating to 5.5.2 and it broke the maven build, current version dates back to 2017...
    junit-platform-commons is in the current dependency tree also, but 1.0.2.

    Also noticed I cannot reproduce this issue on Eclipse EE but only on Eclipse Java, so is problably eclipse edition related. Will test this by managing the junit-platforms-commons version instead and see what that might bring.

  7. kaiwinter commented on Oct 17, 2019

    @kaiwinter
    Contributor

    Updating to 5.5.2 works for me. In addition junit-jupiter-api have to be added in the parent pom because spring-boot-dependencies imports and old version of this.

    Now the tests run on the command line as well as from eclipse. I'll submit a pull request.

    command line

    eclipse

    A future improvement may be to remove all junit-jupiter-* dependencies and replace them by the aggregator dependency junit-jupiter which was introduces with 5.4.0.

  8. oconnelc commented on Oct 17, 2019

    @oconnelc
    ContributorAuthor

    Another option is to switch to TestNG.

  9. shahnawazm786 commented on Jan 13, 2021

    @shahnawazm786

    java.lang.BootstrapMethodError: java.lang.NoClassDefFoundError: org/junit/platform/engine/EngineDiscoveryListener
    at org.junit.platform.launcher.core.LauncherDiscoveryRequestBuilder.getLauncherDiscoveryListener(LauncherDiscoveryRequestBuilder.java:241)
    at org.junit.platform.launcher.core.LauncherDiscoveryRequestBuilder.build(LauncherDiscoveryRequestBuilder.java:235)
    at org.eclipse.jdt.internal.junit5.runner.JUnit5TestLoader.createUnfilteredTest(JUnit5TestLoader.java:75)
    at org.eclipse.jdt.internal.junit5.runner.JUnit5TestLoader.createTest(JUnit5TestLoader.java:66)
    at org.eclipse.jdt.internal.junit5.runner.JUnit5TestLoader.loadTests(JUnit5TestLoader.java:53)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:526)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:770)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:464)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:210)
    Caused by: java.lang.NoClassDefFoundError: org/junit/platform/engine/EngineDiscoveryListener
    at java.lang.ClassLoader.defineClass1(Native Method)
    at java.lang.ClassLoader.defineClass(Unknown Source)
    at java.security.SecureClassLoader.defineClass(Unknown Source)
    at java.net.URLClassLoader.defineClass(Unknown Source)
    at java.net.URLClassLoader.access$100(Unknown Source)
    at java.net.URLClassLoader$1.run(Unknown Source)
    at java.net.URLClassLoader$1.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    ... 9 more
    Caused by: java.lang.ClassNotFoundException: org.junit.platform.engine.EngineDiscoveryListener
    at java.net.URLClassLoader.findClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    ... 21 more

  10. added this to the 1.24.0 milestone on Jan 16, 2021
  11. added a commit that references this issue on Jul 4, 2024
    880b234
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions