Sts4: STS 4.6.0 Eclipse - Springboot 2.2.5 - JUnit5 failing (NoClassDefFoundError)

Created on 25 Mar 2020  ·  8Comments  ·  Source: spring-projects/sts4

Hello,

Launching tests with JUnit5 fails for springboot applications (2.2.5).

Reason:
Eclipse 4.15 ships with JUnit5 5.6.0
Springboot 2.2.5 uses JUnit5 5.5.2 (https://github.com/spring-projects/spring-boot/blob/2.2.x/spring-boot-project/spring-boot-dependencies/pom.xml)

Result:

java.lang.NoClassDefFoundError: org/junit/platform/engine/EngineDiscoveryListener
    at java.base/java.lang.ClassLoader.defineClass1(Native Method)
    at java.base/java.lang.ClassLoader.defineClass(ClassLoader.java:1016)
    at java.base/java.security.SecureClassLoader.defineClass(SecureClassLoader.java:174)
    at java.base/jdk.internal.loader.BuiltinClassLoader.defineClass(BuiltinClassLoader.java:800)
    at java.base/jdk.internal.loader.BuiltinClassLoader.findClassOnClassPathOrNull(BuiltinClassLoader.java:698)
    at java.base/jdk.internal.loader.BuiltinClassLoader.loadClassOrNull(BuiltinClassLoader.java:621)
    at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:579)
    at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178)
    at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:521)
    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.ClassNotFoundException: org.junit.platform.engine.EngineDiscoveryListener
    at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:581)
    at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178)
    at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:521)
    ... 18 more

Solution/Workaround:

adding/updating Junit5 version to 5.6.0 in build.gradle:

ext['junit-jupiter.version'] = '5.6.0'

I think that a warning on the download page would be nice until springboot updates their dependencies to JUnit5 5.6.0.

duplicate bug

Most helpful comment

Another solution is, adding
testImplementation 'org.junit.platform:junit-platform-launcher'
instead of
ext['junit-jupiter.version'] = '5.6.0'
to build.gradle.

@martinlippert
Thank you for moving this issue to the right project.

All 8 comments

We should discuss with the Eclipse team that works on the JUnit integration what the proposed solution is for projects that do not use the exact same version of JUnit5 than the IDE. I think this is a quite common case and many people will hit this.

(oh, and I moved this issue to the Spring Tools 4 project, just for your notice)

(comment in Pivotal Tracker added by Alex Boyko:)

Seems like a problem in Buildship Gradle plugin... If I try the same Maven project with the same dependencies created by Initializr it launches fine and has 5.5.2 JUnit on the classpath... Therefore, seems like something is off with the Eclipse project produced by Buildship Gradle...

(comment in Pivotal Tracker added by Alex Boyko:)

Not sure what's wrong... Probably has to do with the classpath calculated in the launch config by the JDT based on the info in the project... Think this is something to figure out on the Buildship Gradle side hence i raised:
https://github.com/eclipse/buildship/issues/980

Another solution is, adding
testImplementation 'org.junit.platform:junit-platform-launcher'
instead of
ext['junit-jupiter.version'] = '5.6.0'
to build.gradle.

@martinlippert
Thank you for moving this issue to the right project.

Instead of testImplementation 'org.junit.platform:junit-platform-launcher', you can also use testRuntimeOnly 'org.junit.platform:junit-platform-launcher' to keep this away from your compile classpath.

Also note that this is only a _forward compatibility_ issue (the tool uses a newer version than the build). I confirmed with the JUnit team that _backward compatibility_ (the build uses a newer version than the tool) should be guaranteed.

Some more details: https://github.com/eclipse/buildship/issues/980#issuecomment-665124002

我使用使用相同的Eclipse,在一台电脑上可以执行 Junit5 ,另一台电脑上就报错,很奇怪。
Eclipse IDE for Enterprise Java Developers.
Version: 2020-03 (4.15.0)
Build id: 20200313-1211

junit-jupiter 插件版本是 5.6.0

I am closing this one here as a duplicate of the mentioned buildship issue: https://github.com/eclipse/buildship/issues/980. Please feel free to comment if you think we need to keep this open and investigate this further from the Spring Tools side and we can re-open the issue for that, if necessary.

Was this page helpful?
0 / 5 - 0 ratings