NoSuchMethodError: 'java.util.Set org.junit.platform.engine.TestDescriptor.getAncestors()

11.4k Views Asked by At

I get this error while running a JUnit test in Spring Tool Suite (4.20.0).

Test runs fine, but this error is thrown at the end -

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)

This is my Gradle plugins section -

plugins {
    id 'java'
    id 'eclipse'
    id 'eclipse-wtp'
    id 'org.springframework.boot' version "3.1.4"
    id 'io.spring.dependency-management' version "1.1.3"
}

This is my Gradle dependencies section -

implementation("org.springframework.boot:spring-boot-starter-parent:3.1.4")
implementation("org.springframework.boot:spring-boot-starter-web")
implementation("org.springframework.boot:spring-boot-configuration-processor")
implementation("org.springframework.boot:spring-boot-starter-test")

What am I doing wrong?

My test class is very basic -

@ExtendWith(SpringExtension.class)
@ContextConfiguration(initializers = ConfigDataApplicationContextInitializer.class, classes = { TransformService.class})
@Slf4j
public class TransformServiceTest {

    @Autowired
    TransformService transformService;

    @Test
    public void transformXmlTest() throws Exception {
        transformService.transformXml();
    }
}

So, is my service class -

@Service
@Slf4j
public class TransformService {

    public void transformXml() {
        log.info("Transforming XML");
    }
}

I am running the test in STS by right-clicking on "TransformXmlTest()" in the Package Explorer and clicking "Run as - JUnit Test". (When I run it outside STS using Gradle task, everything looks clean).

This is the full log entry -

OpenJDK 64-Bit Server VM warning: Sharing is only supported for boot loader classes because bootstrap classpath has been appended
2023-10-06 10:00:33.097  INFO Transforming XML | com.dowill.xsl.service.TransformService
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)

Thank you for the help

5

There are 5 best solutions below

4
Do Will On BEST ANSWER

I switched to STS 4.19.0 and not seeing this error anymore. Wonder if this is an issue with STS 4.20.0.

I have tagged spring-tools-4 here hoping that someone from the STS community will take a look.

1
aboyko On

This has to with Gradle and its integration into Eclipse (Buildship). You'd get the same behaviour in plain Eclipse for Java Developers.

The solution is add the below to the dependencies in build.gradle:

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

You could also use STS 4.20.0 distro based on Eclipse 4.28 rather than latest 4.29 or look for more workaround in the issue below - very helpful. There is also the explanation what went wrong with eclipse 4.29 distro.

More info can be found here: https://github.com/eclipse/buildship/issues/1265

0
electrobabe On

In my case, I was missing the dependency 'org.junit.jupiter:junit-jupiter' - I only had 'org.junit.jupiter:junit-jupiter-api' in my pom.

Missing the jupiter base dependency caused these errors:

  • In IntelliJ, when I tried to run the tests explicitly: "no tests were found"
  • from command line, running mvn clean install: "there was an error in the forked process 'java.util.set org.junit.platform.engine.testdescriptor.getancestors()'"

0
Aneesh AS On

For following STS:

Spring Tool Suite 4 
Version: 4.21.0.RELEASE
Build Id: 202312011804
Revision: 37b067849b907f022f6dabc8a04af38bbf291249

Below solution got worked:

Update the junit-platform-launcher dependency to 1.8.2 i.e

<dependency>
     <groupId>org.junit.platform</groupId>
     <artifactId>junit-platform-launcher</artifactId>
     <version>1.8.2</version>
     <scope>test</scope>
</dependency>
0
Max On

I had the same problem on Eclipse for Java. I solved it by migrating on pom from junit 5.9.2 to 5.10.2