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

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

Closed
ahmedyarub opened this issue Feb 12, 2019 · 21 comments

Comments

@ahmedyarub
Copy link

@ahmedyarub ahmedyarub commented Feb 12, 2019

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

@ahmedyarub

This comment has been minimized.

Copy link
Author

@ahmedyarub ahmedyarub commented Feb 12, 2019

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>
@ahmedyarub ahmedyarub closed this Feb 12, 2019
@sbrannen

This comment has been minimized.

Copy link
Member

@sbrannen sbrannen commented Feb 12, 2019

Glad you sorted it out!

Thanks for letting us know.

@kjetiljd

This comment has been minimized.

Copy link

@kjetiljd kjetiljd commented Feb 21, 2019

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 ...

@sbrannen

This comment has been minimized.

Copy link
Member

@sbrannen sbrannen commented Feb 21, 2019

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?

@oboehm

This comment has been minimized.

Copy link

@oboehm oboehm commented Apr 1, 2019

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.

@baev

This comment has been minimized.

Copy link
Contributor

@baev baev commented Apr 3, 2019

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

@marcphilipp

This comment has been minimized.

Copy link
Member

@marcphilipp marcphilipp commented Apr 5, 2019

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

@baev

This comment has been minimized.

Copy link
Contributor

@baev baev commented Apr 5, 2019

@marcphilipp roger that, sir!

gradle/gradle#8956

@sormuras

This comment has been minimized.

Copy link
Member

@sormuras sormuras commented Apr 5, 2019

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

@DineshNO

This comment has been minimized.

Copy link

@DineshNO DineshNO commented Sep 7, 2019

@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

@sbrannen

This comment has been minimized.

Copy link
Member

@sbrannen sbrannen commented Sep 7, 2019

@DineshNO, are you using Spring Boot?

If so, this is the answer to your question.

@DineshNO

This comment has been minimized.

Copy link

@DineshNO DineshNO commented Sep 7, 2019

@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

@ahmedyarub

This comment has been minimized.

@DineshNO

This comment has been minimized.

Copy link

@DineshNO DineshNO commented Sep 8, 2019

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.

@sbrannen

This comment has been minimized.

Copy link
Member

@sbrannen sbrannen commented Sep 8, 2019

@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

This comment has been minimized.

Copy link

@DineshNO DineshNO commented Sep 8, 2019

@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

@ahmedyarub

This comment has been minimized.

Copy link
Author

@ahmedyarub ahmedyarub commented Sep 9, 2019

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.

@ahmedyarub

This comment has been minimized.

Copy link
Author

@ahmedyarub ahmedyarub commented Sep 9, 2019

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

@ahmedyarub

This comment has been minimized.

Copy link
Author

@ahmedyarub ahmedyarub commented Sep 11, 2019

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.

@DineshNO

This comment has been minimized.

Copy link

@DineshNO DineshNO commented Sep 11, 2019

That worked with above configuration.Thanks

dfa1 added a commit to dfa1/hosh that referenced this issue Nov 29, 2019
As documented here:
junit-team/junit5#1773
@marcellodesales

This comment has been minimized.

Copy link

@marcellodesales marcellodesales commented Dec 6, 2019

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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
9 participants
You can’t perform that action at this time.