Running JUnit test with JVM 1.6 under Eclipse 2022-03 (4.23.0)

612 Views Asked by At

I have a legacy application that absolutely must compile and run in JVM 1.6.

But it not enough to specify -source 6 -target 6 to a newer compiler (8, 11, 16 whatever JDK version newer than 1.6).

I really need to develop and test in a "concrete"† JVM 1.6, in my case: OpenJDK 64-Bit Server VM (build 23.25-b01, mixed mode), because the server hosting this application cannot get a JVM update.

I've managed to configure maven to do it all (the maven.compiler.* properties and the maven-toolchains-plugin) and everything works fine (mvn test -wise):

[...]
[INFO]
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ [...] ---
[INFO] Toolchain in surefire-plugin: JDK[openjdk-1.6.0-unofficial-b31-windows-amd64-image]
[INFO] Surefire report directory: [...]\target\surefire-reports

-------------------------------------------------------
 T E S T S
-------------------------------------------------------
Running [...]Test
Tests run: 18, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.149 sec
[...]

To be really comfortable however, I need to run the unit tests (junit 4.13.2 + mockito-inline 2.8.47 + powermock 1.7.4) inside Eclipse. But it errors with the following output:

java.lang.UnsupportedClassVersionError: org/eclipse/jdt/internal/junit/runner/RemoteTestRunner : Unsupported major.minor version 52.0
    at java.lang.ClassLoader.defineClass1(Native Method)
    at java.lang.ClassLoader.defineClass(ClassLoader.java:643)
    at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
    at java.net.URLClassLoader.defineClass(URLClassLoader.java:277)
    at java.net.URLClassLoader.access$000(URLClassLoader.java:73)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:212)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:205)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:323)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:294)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:268)
Exception in thread "main" 

I can only guess that Eclipse tries to inject some TestRunner (indeed a RemoteTestRunner) in my application classpath when running the tests, which was compiled under for a newer JVM (class file version 52, which IIRC is compatible with java 8 upwards).

Is there a official/unofficial configuration or workaround for this?

† It's fun to apply "concrete" quality to a "virtual" machine haha.

PS: In vscode I get the very same error (yes, even the "org/eclipse/jdt/internal/junit/runner/RemoteTestRunner" part and the line numbers in stacktrace), with the addition of the message "Could not find the main class: org.eclipse.jdt.internal.junit.runner.RemoteTestRunner. Program will exit.".

0

There are 0 best solutions below