AttachNotSupportedException when running jMockit tests on IBM JRE

I get the following exception when I try to run a simple jMockit / JUnit test using the IBM JDK. Has anyone encountered this issue? I tried setting -Dcom.ibm.tools.attach.enable=yes as a VM argument, but no luck. The same code works fine in the 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 to give the VM -javaagent:jmockit.jar , as suggested by Kevin Welker, and I got the following 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 
+4
source share
6 answers

The Attach API just doesn't work on IBM JDK 6.0, at least on Windows. Therefore, you must use the -javaagent:jmockit.jar .

NullPointerException MockClassSetup.java:59 , when using -javaagent , caused an error in the IBM JDK. If the type specified in the annotation attribute ( @MockClass , in this case) is not in the classpath, the JDK should throw a TypeNotPresentException . Oracle JDKs do as expected, but the IBM JDK returns null instead of the attribute value.

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

+9
source

Above answer to using -javaagent is correct. If you use maven, this is a bit complicated, so here is how I did it:

  • Add the maven-dependency plugin so you can generate absolute dependency paths:
 <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 the 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. In addition, you do not have to, but I would recommend using a relatively new version of jmockit. These problems were discovered in version 1.1 (@ Rogério fixed before September 2012, but adding -javaagent fixes it independently. For reference, I use 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> 
+2
source

Some versions of the IBM JDK do not support the attachment mechanism. You can try running the -javaagent parameter: jmockit.jar. The following information is a bit outdated, but 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

+1
source

I also tried using JMockit to override the final methods in a class in a third-party library. I got the same problem mentioned above and again adding VM arg -javaagent:C:/<Path-to-jar>/jmockit.jar gave me a different type of error. I also use IBM JDK 6.0, which comes with Websphere. This is a shame, but it seems like a real impossibility to redefine the final methods: I cannot find any other testing structure that can do this.

0
source

You just need to upgrade your JDK to 1.8. I tested this version of the JDK and this problem was resolved.

0
source

The setup for javagent for surefire using maven is as follows:

  <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 that 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, change the argLine value accordingly.

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

0
source

Source: https://habr.com/ru/post/1200902/


All Articles