Open howlger opened 11 months ago
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:
With Maven this is no problem, because m2e adds the missing JUnit launcher with a matching version:
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.
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/
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:JUnit 5 test to reproduce this issue:
Following stack trace will be printed to the Console view:
Workarounds:
build.gradle
, add the dependencytestImplementation 'org.junit.platform:junit-platform-launcher:1.9.3'
ortestImplementation 'org.junit.jupiter:junit-jupiter:5.10.0'
or-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 thejunit-platform-launcher
version that is shipped with Eclipse. And in Eclipse 4.29junit-platform-launcher
has been upgraded to 1.10.0. Butorg.junit.platform:junit-platform-launcher:1.10.0
callsorg.junit.platform.engine.TestDescriptor.html::getAncestors
which has been introduced inorg.junit.platform:junit-platform-engine:1.10.0
and which is missing inorg.junit.platform:junit-platform-engine:1.9.3
in JUnit 5.9.3.See also the following Stack Overflow questions: