AttachNotSupportedException while running jMockit

2020-02-12 11:58发布

问题:

I am getting the below exception when I try to run a simple jMockit/JUnit test using IBM JDK. Has anyone faced this issue? I tried given -Dcom.ibm.tools.attach.enable=yes as VM argument but no luck. The same code is working fine in Sun JDK.

java.lang.RuntimeException: com.sun.tools.attach.AttachNotSupportedException: Unable to enqueue operation: the target VM does not support attach mechanism
    at mockit.internal.startup.JDK6AgentLoader.getVirtualMachineImplementationFromEmbeddedOnes(JDK6AgentLoader.java:89)
    at mockit.internal.startup.JDK6AgentLoader.loadAgent(JDK6AgentLoader.java:54)
    at mockit.internal.startup.AgentInitialization.initializeAccordingToJDKVersion(AgentInitialization.java:21)
    at mockit.internal.startup.Startup.initializeIfNeeded(Startup.java:98)
    at mockit.internal.startup.Startup.initializeIfPossible(Startup.java:112)
    at org.junit.runner.Runner.<clinit>(Runner.java:22)
    at java.lang.J9VMInternals.initializeImpl(Native Method)
    at java.lang.J9VMInternals.initialize(J9VMInternals.java:200)
    at java.lang.J9VMInternals.initialize(J9VMInternals.java:167)
    at java.lang.J9VMInternals.initialize(J9VMInternals.java:167)
    at org.junit.internal.builders.JUnit4Builder.runnerForClass(JUnit4Builder.java:13)
    at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:57)
    at org.junit.internal.builders.AllDefaultPossibilitiesBuilder.runnerForClass(AllDefaultPossibilitiesBuilder.java:29)
    at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:57)
    at org.junit.internal.requests.ClassRequest.getRunner(ClassRequest.java:24)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.<init>(JUnit4TestReference.java:33)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestClassReference.<init>(JUnit4TestClassReference.java:25)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.createTest(JUnit4TestLoader.java:48)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.loadTests(JUnit4TestLoader.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:452)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
Caused by: com.sun.tools.attach.AttachNotSupportedException: Unable to enqueue operation: the target VM does not support attach mechanism
    at sun.tools.attach.WindowsVirtualMachine.<init>(WindowsVirtualMachine.java:64)
    at mockit.internal.startup.JDK6AgentLoader.getVirtualMachineImplementationFromEmbeddedOnes(JDK6AgentLoader.java:73)
    ... 22 more
java.lang.NoClassDefFoundError: org.junit.internal.runners.ErrorReportingRunner (initialization failure)
    at java.lang.J9VMInternals.initialize(J9VMInternals.java:140)
    at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:59)
    at org.junit.internal.requests.ClassRequest.getRunner(ClassRequest.java:24)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.<init>(JUnit4TestReference.java:33)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestClassReference.<init>(JUnit4TestClassReference.java:25)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.createTest(JUnit4TestLoader.java:48)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.loadTests(JUnit4TestLoader.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:452)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
Caused by: java.lang.NoClassDefFoundError: org.junit.runner.Runner (initialization failure)
    at java.lang.J9VMInternals.initialize(J9VMInternals.java:140)
    at java.lang.J9VMInternals.initialize(J9VMInternals.java:167)
    at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:59)
    at org.junit.internal.builders.AllDefaultPossibilitiesBuilder.runnerForClass(AllDefaultPossibilitiesBuilder.java:29)
    at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:57)
    ... 9 more
Caused by: java.lang.RuntimeException: com.sun.tools.attach.AttachNotSupportedException: Unable to enqueue operation: the target VM does not support attach mechanism
    at mockit.internal.startup.JDK6AgentLoader.getVirtualMachineImplementationFromEmbeddedOnes(JDK6AgentLoader.java:89)
    at mockit.internal.startup.JDK6AgentLoader.loadAgent(JDK6AgentLoader.java:54)
    at mockit.internal.startup.AgentInitialization.initializeAccordingToJDKVersion(AgentInitialization.java:21)
    at mockit.internal.startup.Startup.initializeIfNeeded(Startup.java:98)
    at mockit.internal.startup.Startup.initializeIfPossible(Startup.java:112)
    at org.junit.runner.Runner.<clinit>(Runner.java:22)
    at java.lang.J9VMInternals.initializeImpl(Native Method)
    at java.lang.J9VMInternals.initialize(J9VMInternals.java:200)
    at java.lang.J9VMInternals.initialize(J9VMInternals.java:167)
    at java.lang.J9VMInternals.initialize(J9VMInternals.java:167)
    at org.junit.internal.builders.JUnit4Builder.runnerForClass(JUnit4Builder.java:13)
    at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:57)
    ... 11 more
Caused by: com.sun.tools.attach.AttachNotSupportedException: Unable to enqueue operation: the target VM does not support attach mechanism
    at sun.tools.attach.WindowsVirtualMachine.<init>(WindowsVirtualMachine.java:64)
    at mockit.internal.startup.JDK6AgentLoader.getVirtualMachineImplementationFromEmbeddedOnes(JDK6AgentLoader.java:73)
    ... 22 more

I tried giving the VM argument -javaagent:jmockit.jar as suggested by Kevin Welker and I got the below exception.

Exception in thread "main" java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:48)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:37)
    at java.lang.reflect.Method.invoke(Method.java:600)
    at sun.instrument.InstrumentationImpl.loadClassAndStartAgent(InstrumentationImpl.java:335)
    at sun.instrument.InstrumentationImpl.loadClassAndCallPremain(InstrumentationImpl.java:350)
Caused by: java.lang.NullPointerException
    at mockit.internal.annotations.MockClassSetup.validateRealClass(MockClassSetup.java:59)
    at mockit.internal.annotations.MockClassSetup.<init>(MockClassSetup.java:38)
    at mockit.internal.annotations.MockClassSetup.<init>(MockClassSetup.java:77)
    at mockit.internal.annotations.MockClassSetup.<init>(MockClassSetup.java:99)
    at mockit.internal.startup.JMockitInitialization.setUpInternalStartupMock(JMockitInitialization.java:56)
    at mockit.internal.startup.JMockitInitialization.initialize(JMockitInitialization.java:29)
    at mockit.internal.startup.Startup.initialize(Startup.java:68)
    at mockit.internal.startup.Startup.premain(Startup.java:56)
    ... 6 more

Fatal error: processing of -javaagent failed

回答1:

The Attach API simply doesn't work in the IBM JDK 6.0, at least on Windows. Therefore, it's necessary to use the -javaagent:jmockit.jar parameter.

The NullPointerException that occurs at MockClassSetup.java:59, when using -javaagent, is caused by a bug in the IBM JDK. When a type referenced in an annotation attribute (@MockClass, in this case) is not present in the classpath, the JDK should throw a TypeNotPresentException. The Oracle JDKs do so as expected, but the IBM JDK returns null instead for the attribute value.

I just implemented a workaround for this in class MockClassSetup, which will be available in the next JMockit release, by the end of september. For now, you can avoid the problem by adding TestNG to the classpath (since the "type not found" is the org.testng.TestNG class).



回答2:

Above answer to use -javaagent is correct. If you're using maven it's a little tricky, so here is how I did it:

  1. Add the maven-dependency-plugin so you can generate absolute paths to the dependencies:
<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-dependency-plugin</artifactId>
  <version>2.5.1</version>
  <executions>
    <execution>
      <id>getClasspathFilenames</id>
      <goals>
         <goal>properties</goal>
      </goals>
    </execution>
  </executions>
</plugin>

2. Add -javaagent to surefire plugin

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-surefire-plugin</artifactId>
  <version>2.13</version>
  <configuration>
    <argLine>-javaagent:${com.googlecode.jmockit:jmockit:jar} -XX:-UseSplitVerifier</argLine>
  </configuration>
</plugin>

3. Also, you don't have to, but I'd recommend using a relatively newer version of jmockit. This issues was detected in 1.1 (prior to September 2012 fix by @Rogério, but adding -javaagent fixes it regardless. For reference I am using the latest version available in maven central (2.5) as of this comment:

<dependency>
  <groupId>com.googlecode.jmockit</groupId>
  <artifactId>jmockit</artifactId>
  <!-- Use latest version. 1.1 gives AttachNotSupportedException -->
  <version>1.5</version>
  <scope>test</scope>
</dependency>


回答3:

Certain versions of the IBM JDK do not properly support the attach mechanism. You might try running with -javaagent:jmockit.jar parameter. The following info is a bit old, but it may still apply as other JDKs do not necessarily fully support the attach mechanism.

See this: http://code.google.com/p/jmockit/issues/detail?id=18



回答4:

I also tried using JMockit to override final methods in a class in a 3rd party library. I got the same problem discussed above, and again adding the VM arg -javaagent:C:/<Path-to-jar>/jmockit.jar only gave me a different type of error. I am also using the IBM JDK 6.0 that comes with Websphere. It's a shame but it looks like it is an actual impossibility to override final methods: I can't find any other Testing framework that can do it.



回答5:

You just need update the JDK to 1.8. I tested this version JDK, and this problem has been resolved.



回答6:

The setup for javagent for surefire using maven looks like this:

 <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>2.18.1</version>
    <configuration>
        ....
        <argLine>-javaagent:"${settings.localRepository}"/org/jmockit/jmockit/1.11/jmockit-1.11.jar</argLine>
    </configuration>
  </plugin>

The above assumes you are using the following dependency:

 <dependency>
    <groupId>org.jmockit</groupId>
    <artifactId>jmockit</artifactId>
    <version>1.11</version>
    <scope>test</scope>
  </dependency>

If you are using a different version then modify the argLine value appropriately.

Source: http://jmockit.googlecode.com/svn-history/r1166/trunk/www/installation.html