AttachNotSupportedException而在IBM JRE运行jMockit测试(At

2019-06-27 15:15发布

我得到下面的异常,当我尝试运行使用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

我试着给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

Answer 1:

在附加API根本不会在IBM JDK 6.0的工作,至少在Windows上。 因此,有必要使用-javaagent:jmockit.jar参数。

所述NullPointerException发生在MockClassSetup.java:59 ,使用时-javaagent ,通过在IBM JDK的错误引起的。 当在注释属性(引用的类型@MockClass ,在这种情况下)不存在于类路径中,JDK应该抛出一个TypeNotPresentException 。 甲骨文的JDK这样做符合市场预期,但IBM JDK返回null ,而不是该属性值。

我刚刚实施了类解决类似的问题MockClassSetup ,这将是在未来JMockit版本中提供,由九月底。 现在,您可以通过添加TestNG的classpath中避免问题(因为“类型未找到”是org.testng.TestNG类)。



Answer 2:

以上回答使用-javaagent是正确的。 如果你正在使用maven这是一个有点棘手,所以这里是我是如何做的:

  1. 添加Maven的依赖,插件,这样你就可以生成依赖绝对路径:
<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来保命插件

<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(之前的2012年9月通过修复@罗热检测到此问题,但无论加入-javaagent修复它作为参考。我正在使用可用的最新版本的Maven中央(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>


Answer 3:

在IBM JDK的某些版本不正确地支持连接机制。 你可以尝试用-javaagent运行:jmockit.jar参数。 下面的信息是有点老了,但它仍然可以适用其他的JDK不一定完全支持连接机制。

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



Answer 4:

我使用JMockit覆盖在第三方库中的一类final方法也试过。 我得到上面讨论的同样的问题,并再次加入VM ARG -javaagent:C:/<Path-to-jar>/jmockit.jar只给了我一不同类型的错误。 我也使用IBM JDK 6.0附带的Websphere。 这是一个耻辱,但它看起来是一个实际的不可能重写final方法:我无法找到任何其他测试框架,可以做到这一点。



Answer 5:

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



Answer 6:

对于javagent的万无一失使用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



文章来源: AttachNotSupportedException while running jMockit tests on IBM JRE