4

当我尝试使用 IBM JDK 运行简单的 jMockit/JUnit 测试时,出现以下异常。有没有人遇到过这个问题?我尝试-Dcom.ibm.tools.attach.enable=yes作为 VM 参数给出,但没有运气。相同的代码在 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

我尝试按照 Kevin Welker 的建议给出 VM 参数-javaagent:jmockit.jar,但得到了以下异常。

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
4

6 回答 6

9

附加 API 在 IBM JDK 6.0 中根本不起作用,至少在 Windows 上是这样。因此,有必要使用该-javaagent:jmockit.jar参数。

使用时NullPointerException发生的是由 IBM JDK 中的错误引起的。当注释属性(在本例中为 )中引用的类型不存在于类路径中时,JDK 应该抛出一个. Oracle JDK 按预期执行此操作,但 IBM JDK改为返回属性值。MockClassSetup.java:59-javaagent@MockClassTypeNotPresentExceptionnull

我刚刚在 class 中实现了一个解决方法MockClassSetup,它将在 9 月底之前在下一个 JMockit 版本中提供。现在,您可以通过将 TestNG 添加到类路径来避免该问题(因为“找不到类型”是org.testng.TestNG类)。

于 2012-08-31T16:37:36.297 回答
2

以上使用 -javaagent 的答案是正确的。如果您使用的是 maven,这有点棘手,所以我是这样做的:

  1. 添加 maven-dependency-plugin 以便您可以生成依赖项的绝对路径:
<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.添加-javaagent到surefire插件

<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. 另外,您不必这样做,但我建议使用相对较新版本的 jmockit。这个问题是在 1.1 中检测到的(@Rogério 在 2012 年 9 月修复之前,但添加 -javaagent 无论如何都会修复它。作为参考,我正在使用 maven Central(2.5)中可用的最新版本作为此评论:

<dependency>
  <groupId>com.googlecode.jmockit</groupId>
  <artifactId>jmockit</artifactId>
  <!-- Use latest version. 1.1 gives AttachNotSupportedException -->
  <version>1.5</version>
  <scope>test</scope>
</dependency>
于 2013-12-05T16:30:46.620 回答
1

某些版本的 IBM JDK 不正确地支持附加机制。您可以尝试使用 -javaagent:jmockit.jar 参数运行。以下信息有点旧,但它可能仍然适用,因为其他 JDK 不一定完全支持附加机制。

看到这个:http ://code.google.com/p/jmockit/issues/detail?id=18

于 2012-08-31T03:03:02.237 回答
0

我还尝试使用 JMockit 覆盖 3rd 方库中的类中的最终方法。我遇到了上面讨论的相同问题,再次添加 VM arg-javaagent:C:/<Path-to-jar>/jmockit.jar只会给我一个不同类型的错误。我也在使用 Websphere 附带的 IBM JDK 6.0。很遗憾,但看起来实际上不可能覆盖最终方法:我找不到任何其他测试框架可以做到这一点。

于 2013-06-19T03:54:33.283 回答
0

您只需要将 JDK 更新到 1.8。我测试了这个版本的JDK,这个问题已经解决了。

于 2014-11-04T03:12:54.253 回答
0

javagent for surefire 使用 maven 的设置如下所示:

 <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>

以上假设您正在使用以下依赖项:

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

如果您使用不同的版本,请适当修改 argLine 值。

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

于 2015-01-26T15:50:42.950 回答