1
votes

Junit 5 test cases for a Spring boot application 2.1.7.RELEASE fails build with below error in intelij.

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.22.2:test (default-test) on project domain: Execution default-test of goal org.apache.maven.plugins:maven-surefire-plugin:2.22.2:test failed: Unsupported class file major version 56 -> [Help 1]

using java 9 modularization

Application builds in maven and works without test cases.

used maven surefire plugin version 2.22.1 fails in command line and intellij

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <configuration>
                <useSystemClassLoader>false</useSystemClassLoader>
            </configuration>
        </plugin>



        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <configuration>
                <jvmArguments>--add-modules=java.xml.ws.annotation</jvmArguments>
            </configuration>
        </plugin>
3
this means you are trying to run code on a JVM that's older than the one used compiling the code. For instance: you write Java 8 code, compile it with a Java 8 JDK and then continue to try and run it on a Java 6 JVM. (with you it are different versions, but you get the drift, I hope)Stultuske
Have you looked at this similar [question] (stackoverflow.com/questions/55303837/…)Arnaud Claudel
Do you understand what the message " Unsupported class file major version 56" means?Thorbjørn Ravn Andersen
i have built the application in intellij with jdk 12 and it was successful.After adding the junit 5 i am facing this issue. Unsupported class file major version 56 refers to jdk 12 .Dinesh
i can run the test cases too successfully.It fails in maven buildDinesh

3 Answers

2
votes

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.

0
votes

The JVM being executed here in the Maven build is not the one you think it is. You need Java 12 to execute your compiled code, and it appears that for some reason Maven uses Java 11.

0
votes

This is caused by an old version of asm that the maven-surefire-plugin dependes on.

if you run your maven build using mvn test-X you should see that a version 6.x of asm is used for the plugin.

That older version is not capable of using reflection on newer byte code, indepdendent of the executing runtime, which is what actually causes that problem.

try this, which worked for me:

       <plugin>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.22.2</version>
            <dependencies>
                <dependency>
                    <groupId>org.ow2.asm</groupId>
                    <artifactId>asm</artifactId>
                    <version>7.3.1</version>
                </dependency>
            </dependencies>
        </plugin>

you may need to add that to your parent-pom's dependency-management configuration in order to make sure it is applied to all sub-projects properly.