Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NoSuchMethodError when running a JUnit 5.9.3 test in Eclipse 2023-09 (4.29) #1265

Open
howlger opened this issue Sep 21, 2023 · 2 comments
Open

Comments

@howlger
Copy link

howlger commented Sep 21, 2023

Running a JUnit 5.9.3 test (e.g. via right-click and choosing Run As > JUnit Test) fails in Eclipse 2023-09 (4.29) with NoSuchMethodError.

build.gradle to reproduce this issue:

apply plugin: 'java'
java {
    sourceCompatibility = JavaVersion.VERSION_17
    targetCompatibility = JavaVersion.VERSION_17
}
repositories {
    mavenCentral()
}
dependencies {    
    testImplementation 'org.junit.jupiter:junit-jupiter:5.9.3'
}

JUnit 5 test to reproduce this issue:

class Sample {
	@org.junit.jupiter.api.Test
	void test() {
		org.junit.jupiter.api.Assertions.assertTrue(true);
	}
}

Following stack trace will be printed to the Console view:

java.lang.NoSuchMethodError: 'java.util.Set org.junit.platform.engine.TestDescriptor.getAncestors()'
	at org.junit.platform.launcher.core.StackTracePruningEngineExecutionListener.getTestClassNames(StackTracePruningEngineExecutionListener.java:50)
	at org.junit.platform.launcher.core.StackTracePruningEngineExecutionListener.executionFinished(StackTracePruningEngineExecutionListener.java:39)
	at org.junit.platform.launcher.core.DelegatingEngineExecutionListener.executionFinished(DelegatingEngineExecutionListener.java:46)
	at org.junit.platform.launcher.core.OutcomeDelayingEngineExecutionListener.reportEngineFailure(OutcomeDelayingEngineExecutionListener.java:83)
	at org.junit.platform.launcher.core.EngineExecutionOrchestrator.execute(EngineExecutionOrchestrator.java:203)
	at org.junit.platform.launcher.core.EngineExecutionOrchestrator.execute(EngineExecutionOrchestrator.java:169)
	at org.junit.platform.launcher.core.EngineExecutionOrchestrator.execute(EngineExecutionOrchestrator.java:93)
	at org.junit.platform.launcher.core.EngineExecutionOrchestrator.lambda$execute$0(EngineExecutionOrchestrator.java:58)
	at org.junit.platform.launcher.core.EngineExecutionOrchestrator.withInterceptedStreams(EngineExecutionOrchestrator.java:141)
	at org.junit.platform.launcher.core.EngineExecutionOrchestrator.execute(EngineExecutionOrchestrator.java:57)
	at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:103)
	at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:94)
	at org.junit.platform.launcher.core.DelegatingLauncher.execute(DelegatingLauncher.java:52)
	at org.junit.platform.launcher.core.SessionPerRequestLauncher.execute(SessionPerRequestLauncher.java:70)
	at org.eclipse.jdt.internal.junit5.runner.JUnit5TestReference.run(JUnit5TestReference.java:98)
	at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:40)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:529)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:756)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:452)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:210)

Workarounds:

  • In build.gradle, add the dependency testImplementation 'org.junit.platform:junit-platform-launcher:1.9.3' or
  • Upgrade to JUnit 5.10.0: testImplementation 'org.junit.jupiter:junit-jupiter:5.10.0' or
  • For Spring Boot projects, in the preferences Gradle (for all projects) or in Project > Properties: Gradle (for a specific project) in the section Program Arguments click the Add button and add the program argument -Pjunit-jupiter.version=5.10.0 (see @danielerepici's Stack Overflow answer)

Cause:
It seems Buildship does not choose the corresponding junit-platform-launcher (as m2e seems to do; at least this issue cannot be reproduced with Maven), but uses the junit-platform-launcher version that is shipped with Eclipse. And in Eclipse 4.29 junit-platform-launcher has been upgraded to 1.10.0. But org.junit.platform:junit-platform-launcher:1.10.0 calls org.junit.platform.engine.TestDescriptor.html::getAncestors which has been introduced in org.junit.platform:junit-platform-engine:1.10.0 and which is missing in org.junit.platform:junit-platform-engine:1.9.3 in JUnit 5.9.3.

See also the following Stack Overflow questions:

@oleosterhagen
Copy link
Contributor

oleosterhagen commented Sep 24, 2023

Thank you @howlger for your detailed analyses.

As mentioned, when the junit-platform-launcher is missing on the launch configuration classpath, Eclipse adds its internal junit-platform-launcher, which may be not compatible with the used junit-jupiter version:

https://github.com/eclipse-jdt/eclipse.jdt.ui/blob/055a935ec3f04c17f5b1ba6a2a8c95380ca5226c/org.eclipse.jdt.junit.core/src/org/eclipse/jdt/junit/launcher/JUnitLaunchConfigurationDelegate.java#L200-L208

With Maven this is no problem, because m2e adds the missing JUnit launcher with a matching version:

https://github.com/eclipse-m2e/m2e-core/blob/ac7bf50bb5318285470a065f1981ca4dd91aa241/org.eclipse.m2e.jdt/src/org/eclipse/m2e/jdt/internal/launch/MavenRuntimeClasspathProvider.java#L274-L277

The clean way to solve this problem with Gradle/Buildship is to include the missing org.junit.platform:junit-platform-launcher dependency in build.gradle (see Manually declaring dependencies). This is automatically done when you create a project with the Gradle Build Init Plugin from newer Gradle distributions:

dependencies {
    // Use JUnit Jupiter for testing.
    testImplementation 'org.junit.jupiter:junit-jupiter:5.9.3'

    testRuntimeOnly 'org.junit.platform:junit-platform-launcher'
[...]

Unfortunately, when you do not change the Gradle version in Window > Preferences > Gradle the New Gradle Project Wizard in Buildship uses Gradle 8.1.1 and the Build Init Plugin from this distribution does not add this dependency. In my opinion, this gives not the best first-time user experience.

The execution of JUnit tests on command line or with Run As > Gradle Test is no problem, because then the missing dependency is added at runtime. But this feature is deprecated and will be removed:

> Task :lib:test
The automatic loading of test framework implementation dependencies has been deprecated. This is scheduled to be removed in Gradle 9.0. Declare the desired test framework directly on the test suite or explicitly declare the test framework implementation dependencies on the test's runtime classpath. Consult the upgrading guide for further information: https://docs.gradle.org/8.3/userguide/upgrading_version_8.html#test_framework_implementation_dependencies

To improve the first-time user experience in Eclipse, Buildship should be updated to reference a newer Gradle distribution.

@djkdevise
Copy link

En mi caso particular este error se soluciono siguiendo estos pasos: Una vez abierto el proyecto, haz clic derecho sobre él y elige Properties -> Java Build Path. Activa la pestaña Libraries y haz clic sobre Add Library… Selecciona JUnit -> Next -> Finish -> Apply and close

Recuerda que también puedes añadir la librería a través de Maven, pero yo he añadido la librería de esta forma, que es igual de válida.

Observa que se han añadido todas las librerías correspondientes a JUnit 5 a tu proyecto.

tomado de: https://developrogramming.com/testear-con-junit-5/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants