1

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>
Dinesh
  • 57
  • 2
  • 8
  • 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 Sep 02 '19 at 09:06
  • Have you looked at this similar [question] (https://stackoverflow.com/questions/55303837/problem-running-tests-with-enabled-preview-features-in-surefire-and-failsafe) – Arnaud Claudel Sep 02 '19 at 09:14
  • 1
    Do you understand what the message " Unsupported class file major version 56" means? – Thorbjørn Ravn Andersen Sep 02 '19 at 10:45
  • 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 Sep 02 '19 at 12:41
  • i can run the test cases too successfully.It fails in maven build – Dinesh Sep 02 '19 at 12:41
  • @Arnaud - i have tried adding that --enable-preview maven options but i am still facing this.May be i need to check more on that. – Dinesh Sep 02 '19 at 12:55
  • I have degraded the compiler version : 11 11 now it works. it doesn't work with 12 – Dinesh Sep 02 '19 at 13:08
  • See https://stackoverflow.com/questions/9170832/list-of-java-class-file-format-major-version-numbers – Raedwald Sep 03 '19 at 11:51

3 Answers3

2

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.

Ahmed
  • 162
  • 1
  • 9
  • 1
    Oracle configuration was added for Toolchains configuration to build with maven jlink plugin. – Dinesh Sep 11 '19 at 14:47
1

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.

omilke
  • 827
  • 9
  • 20
0

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.

Thorbjørn Ravn Andersen
  • 73,784
  • 33
  • 194
  • 347
  • maybe.I just wanted to know ,is there any config required in maven to fix the issue with 12 – Dinesh Sep 02 '19 at 14:43
  • Are you absolutely certain that Java 12 is used by Maven? What is the output of `mvn -v`? – Thorbjørn Ravn Andersen Sep 03 '19 at 09:35
  • Java version: 12.0.2, vendor: Oracle Corporation Java home: C:\Program Files\Java\jdk-12.0.2 Default locale: en_US, platform encoding: Cp1252 OS name: "windows 10", version: "10.0", arch: "amd64", family: "windows" – Dinesh Sep 03 '19 at 11:32