Junit5: java.lang.NoSuchMethodError: org.junit.platform.commons.util.ReflectionUtils.tryToLoadClass Migrating to 5.4.0

Created on 12 Feb 2019  路  26Comments  路  Source: junit-team/junit5

JDK: 11
Spring Boot: 2.1.2.RELEASE
JUnit: 5.4.0
Maven: 3.6.0
Surefire: 3.0.0-M3

At first, I got the following error running the tests on IntelliJ and using the command mvn clean install:

[INFO] --- maven-surefire-plugin:3.0.0-M3:test (default-test) @ xxxxx ---
[INFO]
[INFO] -------------------------------------------------------
[INFO]  T E S T S
[INFO] -------------------------------------------------------
Feb 12, 2019 12:53:00 PM org.junit.platform.launcher.core.DefaultLauncher handleThrowable
WARNING: TestEngine with ID 'junit-jupiter' failed to execute tests
java.lang.NoSuchMethodError: org.junit.platform.commons.util.ReflectionUtils.tryToLoadClass(Ljava/lang/String;)Lorg/junit/platform/commons/function/Try;
        at org.junit.jupiter.engine.support.OpenTest4JAndJUnit4AwareThrowableCollector.createAbortedExecutionPredicate(OpenTest4JAndJUnit4AwareThrowableCollecto

After adding this dependency, I could run the tests in IntelliJ but not using Maven:

        <dependency>
            <groupId>org.junit.platform</groupId>
            <artifactId>junit-platform-launcher</artifactId>
            <version>1.4.0</version>
            <scope>test</scope>
        </dependency>

Rolling back to version 5.3.2 solves the problem

Most helpful comment

Fixed by replacing:

        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-api</artifactId>
            <version>${junit.jupiter.version}</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-params</artifactId>
            <version>${junit.jupiter.version}</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-engine</artifactId>
            <version>${junit.jupiter.version}</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.junit.platform</groupId>
            <artifactId>junit-platform-launcher</artifactId>
            <version>1.4.0</version>
            <scope>test</scope>
        </dependency>

with:

        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter</artifactId>
            <version>${junit.jupiter.version}</version>
            <scope>test</scope>
        </dependency>

All 26 comments

Fixed by replacing:

        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-api</artifactId>
            <version>${junit.jupiter.version}</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-params</artifactId>
            <version>${junit.jupiter.version}</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-engine</artifactId>
            <version>${junit.jupiter.version}</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.junit.platform</groupId>
            <artifactId>junit-platform-launcher</artifactId>
            <version>1.4.0</version>
            <scope>test</scope>
        </dependency>

with:

        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter</artifactId>
            <version>${junit.jupiter.version}</version>
            <scope>test</scope>
        </dependency>

Glad you sorted it out!

Thanks for letting us know.

Ran into the same issue - java.lang.NoSuchMethodError: org.junit.platform.commons.util.ReflectionUtils.tryToLoadClass when upgrading from 5.3.2 to 5.4.0 and others seem to have done so, too: https://stackoverflow.com/questions/53926264/nosuchmethoderror-org-junit-platform-commons-util-reflectionutils-trytoloadclas

Something changed between 5.3.2 and 5.4.0 ... and worse ... in my case it failed silently ... so I only noticed because the test that was omitted takes a while to run - so my build sped up a bit - which made me suspicious ...

Yes, this issue has come up several times in conjunction with Spring Boot and its dependency management.

See also:

@kjetiljd, would you mind opening a new GitHub issue so that the JUnit team can improve the diagnostics for such failures?

Same problem - in my case I added the following dependency:

        <dependency>
            <groupId>org.junit.platform</groupId>
            <artifactId>junit-platform-commons</artifactId>
            <version>1.4.1</version>
            <scope>test</scope>
        </dependency>

Works for me.

BTW using Gradle you'll get no warning at all. In our projects we just realised that most important tests are not running for last two weeks

@baev That sounds bad! Is there already an issue for Gradle to improve that? If not, could you please create one?

@marcphilipp roger that, sir!

https://github.com/gradle/gradle/issues/8956

Wonder if JUnit should check "module" versions at runtime and emit an alert if versions are not aligned...

@ahmedyarub

I am trying to run test case with JUnit 5.5.1.

Caused by: java.lang.ClassNotFoundException: org.junit.platform.engine.support.discovery.SelectorResolver.

To resolve this issue, when i searched i found this link and below dependency can be added to resolve.

        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter</artifactId>
            <version>${junit.jupiter.version}</version>
            <scope>test</scope>
        </dependency>

with 5.5.1 version not working using Maven 3.5.x

But it leads to below which makes it impossible to use features like @NullAndEmptySource.

image

Please help

@DineshNO, are you using Spring Boot?

If so, this is the answer to your question.

@DineshNO, are you using Spring Boot?

If so, this is the answer to your question.

Thanks @sbrannen

I am using spring boot 2.1.7.RELEASE and the solution worked great 馃憤

i just want to add another question here:

<maven.compiler.source>12</maven.compiler.source> <maven.compiler.target>12</maven.compiler.target>
when used with junit 5 (using 5.5.1),getting error
Caused by: java.lang.IllegalArgumentException: Unsupported class file major version 56

https://stackoverflow.com/questions/57754113/maven-surefire-plugin-2-22-2-build-fails-for-junit-5-test-cases-with-errror-unsu?noredirect=1#comment101977308_57754113

Hi @ahmedyarub
echo $JAVA_HOME - C:\Program Files\Java\jdk-12.0.2
java -version - java version "1.8.0_221"
javac -version - javac 1.8.0_151

image

<maven.compiler.source>11</maven.compiler.source> <maven.compiler.target>11</maven.compiler.target>
this works.

If i remove my junit dependencies,it works with even 12.

@DineshNO, regarding your latest questions, please allow me to refer you to our policy:

Thanks for getting in touch, but it feels like this is a question that would be better suited to Stack Overflow. We prefer to use the issue tracker only for bugs and enhancements. Feel free to update this issue with a link to the re-posted question (so that other people can find it) or add some more details if you feel this is a genuine bug.

@DineshNO, regarding your latest questions, please allow me to refer you to our policy:

Thanks for getting in touch, but it feels like this is a question that would be better suited to Stack Overflow. We prefer to use the issue tracker only for bugs and enhancements. Feel free to update this issue with a link to the re-posted question (so that other people can find it) or add some more details if you feel this is a genuine bug.

Hi @sbrannen
will do that.Thanks

Hi @ahmedyarub
echo $JAVA_HOME - C:\Program Files\Java\jdk-12.0.2
java -version - java version "1.8.0_221"
javac -version - javac 1.8.0_151

image

<maven.compiler.source>11</maven.compiler.source> <maven.compiler.target>11</maven.compiler.target>
this works.

If i remove my junit dependencies,it works with even 12.

Obviously, your environmental variables (PATH and JAVA_HOME) are pointing to different Java versions. I'll of them should be pointing to the same path.

Hi @ahmedyarub
echo $JAVA_HOME - C:\Program Files\Java\jdk-12.0.2
java -version - java version "1.8.0_221"
javac -version - javac 1.8.0_151
image
<maven.compiler.source>11</maven.compiler.source> <maven.compiler.target>11</maven.compiler.target>
this works.
If i remove my junit dependencies,it works with even 12.

Obviously, your environmental variables (PATH and JAVA_HOME) are pointing to different Java versions. I'll of them should be pointing to the same path.

All of the environmental variables (PATH and JAVA_HOME) should be pointing to the same Java version

Just for reference, @DineshNO's problem was solved this way:

From the error log you can see that the sub-modules are using maven-surefire-plugin 2.22.2
The solution for this is defining the version of Maven plugins to be used by all modules adding this section to the main pom.xml:

<build>
  <pluginManagement>
      <plugins>
          <plugin>
              <groupId>org.apache.maven.plugins</groupId>
              <artifactId>maven-surefire-plugin</artifactId>
              <version>3.0.0-M3</version>
          </plugin>
          <plugin>
              <groupId>org.apache.maven.plugins</groupId>
              <artifactId>maven-compiler-plugin</artifactId>
              <version>3.8.0</version>
              <configuration>
                  <release>12</release>
              </configuration>
          </plugin>
      </plugins>
  </pluginManagement>
</build>

I could compile it after removing the Oracle dependency, I don't know if you will need to do that. If it works for you please comment in the issue.

That worked with above configuration.Thanks

I got the following with SpringBoot gRPC tests

  • Upgraded jacoco and surefire
        <junit-jupiter.version>5.5.2</junit-jupiter.version>
+        <jacoco-maven-plugin.version>0.8.4</jacoco-maven-plugin.version>
         <maven-release-plugin.version>2.5.3</maven-release-plugin.version>
         <maven-deploy-plugin.version>2.8.2</maven-deploy-plugin.version>
-        <maven-surefire-report-plugin.version>2.22.0</maven-surefire-report-plugin.version>
-        <maven-surefire-plugin.version>2.22.0</maven-surefire-plugin.version>
+        <maven-surefire-report-plugin.version>2.22.2</maven-surefire-report-plugin.version>
+        <maven-surefire-plugin.version>2.22.2</maven-surefire-plugin.version>
  • Don't import Engine, and import API if needed
         <dependency>
             <groupId>org.junit.jupiter</groupId>
-            <artifactId>junit-jupiter-api</artifactId>
+            <artifactId>junit-jupiter-api</artifactId> <!-- redundant, but added because IntelliJ 2019.3 + some Eclipse couldn't find classes -->
             <version>${junit-jupiter.version}</version>
             <scope>test</scope>
         </dependency>
         <dependency>
             <groupId>org.junit.jupiter</groupId>
-            <artifactId>junit-jupiter-engine</artifactId>
+            <artifactId>junit-jupiter</artifactId>
             <version>${junit-jupiter.version}</version>
             <scope>test</scope>
         </dependency>
  • Upon looking at effective pom, some dependency is importing an older version of junit. Added the following to the dependency management and got the same exact loaded version of junit
@@ -114,6 +114,13 @@
                 <scope>import</scope>
                 <type>pom</type>
             </dependency>
+            <dependency>
+                <groupId>org.junit</groupId>
+                <artifactId>junit-bom</artifactId>
+                <version>${junit-jupiter.version}</version>
+                <scope>import</scope>
+                <type>pom</type>
+            </dependency>
         </dependencies>
     </dependencyManagement>

when I replaced api, params and engine with
testImplementation("org.junit.jupiter:junit-jupiter:5.7.0-M1")
and trying to use it with @ExtendWith(MockitoExtension::class)
I got this error
java.lang.NoSuchMethodError: 'org.junit.jupiter.api.extension.TestInstances org.junit.jupiter.api.extension.ExtensionContext.getRequiredTestInstances()'
how to solve this issue ?

@Abdelsattar Are you using Spring Boot by any chance? I suspect you still have an older version of junit-jupiter-api on the classpath. Could you please share a build scan?

Update
Solved: I was using different version of mockito-junit-jupiter than mockito

@marcphilipp
yes, I use spring boot
this what I add in the build
` testImplementation("org.springframework.boot:spring-boot-starter-test") {
exclude(group = "org.junit.vintage", module = "junit-vintage-engine")
}

testImplementation("org.junit.jupiter:junit-jupiter:5.7.0-M1")
testImplementation("org.mockito:mockito-junit-jupiter:3.3.3")

`
and when gradle dependcies tree as following;

+--- org.junit.jupiter:junit-jupiter:5.7.0-M1
+--- org.junit:junit-bom:5.7.0-M1
| +--- org.junit.jupiter:junit-jupiter:5.7.0-M1 (c)
| +--- org.junit.jupiter:junit-jupiter-api:5.7.0-M1 -> 5.3.2 (c)
| +--- org.junit.jupiter:junit-jupiter-params:5.7.0-M1 -> 5.3.2 (c)
| --- org.junit.platform:junit-platform-commons:1.7.0-M1 -> 1.3.2 (c)
+--- org.junit.jupiter:junit-jupiter-api:5.7.0-M1 -> 5.3.2
| +--- org.apiguardian:apiguardian-api:1.0.0
| +--- org.opentest4j:opentest4j:1.1.1
| --- org.junit.platform:junit-platform-commons:1.3.2
| --- org.apiguardian:apiguardian-api:1.0.0
--- org.junit.jupiter:junit-jupiter-params:5.7.0-M1 -> 5.3.2
+--- org.apiguardian:apiguardian-api:1.0.0
--- org.junit.jupiter:junit-jupiter-api:5.3.2 (*)

testImplementation("org.junit.jupiter:junit-jupiter:5.7.0-M1")

Do not manually override the version like that, because it's unsupported.

Instead you have to properly override the Spring Boot managed dependency.

You'll find the solution here: https://stackoverflow.com/a/54605523/388980

@Abdelsattar,

Speaking of which, the proper way to override the version with Spring Boot was already covered in this very issue in https://github.com/junit-team/junit5/issues/1773#issuecomment-529110526.

Thus, please make sure you read existing comments before posting new questions.

Was this page helpful?
0 / 5 - 0 ratings