3

Our project uses jmockit 0.999.11 for mocking objects for JUnit 4.11 and TestNG 6.9.4 test classes and suites. We are using Maven as dependency mgmt tool and for compiling and execution of these test cases (Windows OS).

Recently, we have migrated our project from JDK 7 to JDK 8 (1.8.0.25). Since then when I try to execute test cases, jmockit gives me an error saying

Running TestSuite

java.lang.IllegalStateException: JMockit requires a Java 5 VM or later.

I checked various blogs about this and have made sure that classpath has jmockit jar ahead of junit jar. Other instructions about adding jdk tools.jar is specific to Mac OS.

So I am not able to figure out what else is required so that jmockit 0.999.11 can successfully identify JDK 8 (u25). Upgrading JMockit jar would be our last resort and we would like to avoid it as much as possible.

Community
  • 1
  • 1
Aditya G.
  • 119
  • 3
  • 6

2 Answers2

1

The long and short of it is you need to upgrade to a later version of jmockit. When jmockit starts up, it uses this block of code in AgentInitialization:

boolean initializeAccordingToJDKVersion() {
   String jarFilePath = discoverPathToJarFile();

   if (Startup.jdk6OrLater) {
      return new JDK6AgentLoader(jarFilePath).loadAgent();
   } else if ("1.5".equals(Startup.javaSpecVersion)) {
      throw new IllegalStateException("JMockit has not been initialized. Check that your Java 5 VM has been started with the -javaagent:" +
            jarFilePath + " command line option.");
   } else {
      throw new IllegalStateException("JMockit requires a Java 5 VM or later.");
   }
}

And Startup.jdk6OrLater determines that with:

static final boolean jdk6OrLater = ("1.6".equals(javaSpecVersion)) || ("1.7".equals(javaSpecVersion));

public static boolean isJava6OrLater() {
    return jdk6OrLater;
}

You can see the problem. I'll give them the benefit of the doubt in their choice to hard-code the versions that are higher than 1.5, but it's frustrating nonetheless.

It looks like 1.8 works with Java 8. There may be earlier versions that do as well, but 1.8 was working for my cohorts so I decided to stick with that. Alternatively, downgrading to Java 7 should also do the trick.

atraudes
  • 2,368
  • 2
  • 21
  • 31
0

JMockit has a state-of-the-art way to check the java version

static { jdk6OrLater = "1.6".equals(javaSpecVersion) || "1.7".equals(javaSpecVersion) || "1.8".equals(javaSpecVersion); }

This is the reason why it doesn't work if your javaSpecVersion includes a string that is different than any of the above options.