[Fixed] NoSuchMethodError: 'java.util.Set org.junit.platform.engine.TestDescriptor.getAncestors() – Junit

by
Liam Thompson
junit spring-boot spring-tool-suite spring-tools-4

Quick Fix: Downgrade to a previous STS version, such as 4.19.0, since this error appears to be specific to STS 4.20.0.

The Problem:

You are encountering a NoSuchMethodError when running a JUnit test in Spring Tool Suite. The error message specifically mentions ‘java.util.Set org.junit.platform.engine.TestDescriptor.getAncestors()’. This error occurs during test execution and is likely related to a version mismatch or compatibility issue between your project dependencies and the junit platform.

The Solutions:

Solution 1: Downgrade STS version

The issue was fixed by downgrading to STS 4.19.0. This suggests that the error was caused by a bug in STS 4.20.0.

Solution 2: Use STS 4.20.0 or Add ‘testRuntimeOnly ‘org.junit.platform:junit-platform-launcher” Dependency

This issue occurs due to the integration of Gradle and Eclipse (using Buildship). You would encounter the same behavior in Eclipse for Java Developers. The solution is to add the following dependency to the build.gradle file:

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

Alternatively, you can use STS 4.20.0 distro based on Eclipse 4.28 instead of the latest 4.29 version. You could also explore other workarounds mentioned in the GitHub issue linked below.

More information can be found here:

https://github.com/eclipse/buildship/issues/1265

Solution 4: Missing dependency 'org.junit.jupiter:junit-jupiter'

In this solution, the problem was caused by a missing dependency in the project. Specifically, the dependency ‘org.junit.jupiter:junit-jupiter’ was missing, while only ‘org.junit.jupiter:junit-jupiter-api’ was included.

Here’s a more detailed explanation:

  1. Dependency Conflict: Having only ‘org.junit.jupiter:junit-jupiter-api’ in the project caused a dependency conflict. This is because ‘junit-jupiter-api’ is a subproject of ‘junit-jupiter’ and relying on it alone may lead to missing dependencies.

  2. Missing Base Dependency: ‘org.junit.jupiter:junit-jupiter’ is the base dependency for JUnit Jupiter and contains the necessary classes and annotations for writing and running JUnit 5 tests. Without this dependency, the test runner cannot find and execute the test methods.

  3. Error Message Explanation: The error message ‘java.lang.NoSuchMethodError: ‘java.util.Set org.junit.platform.engine.TestDescriptor.getAncestors()” indicates that the test runner is unable to find the ‘getAncestors()’ method on the ‘org.junit.platform.engine.TestDescriptor’ class. This method is used by the JUnit Jupiter engine to gather information about the test hierarchy and ancestry. The absence of this method suggests that the necessary JUnit Jupiter dependency is missing.

To resolve this issue, the missing dependency ‘org.junit.jupiter:junit-jupiter’ should be added to the project’s dependencies. This dependency should be added according to the project’s build system (e.g., Maven, Gradle).

Once the missing dependency is added, the test runner should be able to find the necessary classes and methods, and the tests should run successfully without the ‘NoSuchMethodError’ exception.

Q&A

Why am I getting NoSuchMethodError in STS when running JUnit tests?

Check compatibility between STS version and JUnit libraries.

How to fix NoSuchMethodError in STS 4.20.0?

Add ‘dependency ‘org.junit.platform:junit-platform-launcher to Gradle dependencies.

What is another fix for NoSuchMethodError in STS while running JUnit tests?

Update ‘junit-platform-launcher’ dependency to version 1.8.2.

Video Explanation:

The following video, titled "How To Fix java.lang.NoSuchMethodError In TestNG - YouTube", provides additional insights and in-depth exploration related to the topics discussed in this post.

Play video

If you are facing java.lang.NoSuchMethodError then by just making quick changes in version can fix this issue.