6

I am trying to run JUnit tests from an Ant script. The tests use the JMockit mocking framework, which for Java 5 requires specifying it as a javaagent to run correctly. Here is the script I am running:

<!DOCTYPE project>
<project name="junit_test">
   <property name="PROJECT_PATH" value="{Path to my eclipse project}" />
   <property name="LIB_PATH" value="${PROJECT_PATH}/WebContent/WEB-INF/lib" />
   <property name="TEST_PATH" value="WebContent/WEB-INF/classes" />

   <target name="run_junit">
      <junit fork="yes" forkmode="once" printsummary="true">
         <jvmarg value="-javaagent:${LIB_PATH}/jmockit.jar" />

         <classpath path="${LIB_PATH}/jmockit.jar" />
         <classpath path="${LIB_PATH}/junit-4.8.2.jar" />

         <batchtest>
            <fileset dir="${TEST_PATH}">
               <include name="**/*Test.class"/>
            </fileset>
         </batchtest>
      </junit>

      <junitreport todir="/junitOut">
         <fileset dir="/junitOut">
            <include name="INCOMPLETE-*.xml"/>
            <include name="TEST-*.xml"/>
         </fileset>
      <report todir="/junitOut/html"/>
   </junitreport>
</target>
</project>

I have a feeling that I'm not setting the javaagent correctly. The tests error with this exception:

java.lang.reflect.InvocationTargetException
at java.lang.reflect.Constructor.newInstance(Constructor.java:515)
at org.eclipse.ant.internal.ui.antsupport.EclipseDefaultExecutor.executeTargets(EclipseDefaultExecutor.java:32)
at org.eclipse.ant.internal.ui.antsupport.InternalAntRunner.run(InternalAntRunner.java:423)
at org.eclipse.ant.internal.ui.antsupport.InternalAntRunner.main(InternalAntRunner.java:137)
at java.lang.J9VMInternals.initialize(J9VMInternals.java:140)
at java.lang.J9VMInternals.initialize(J9VMInternals.java:140)
at java.lang.J9VMInternals.initialize(J9VMInternals.java:167)
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.attachToThisVM(JDK6AgentLoader.java:113)
at mockit.internal.startup.JDK6AgentLoader.loadAgent(JDK6AgentLoader.java:77)
at mockit.internal.startup.AgentInitialization.initializeAccordingToJDKVersion(AgentInitialization.java:41)
at mockit.internal.startup.Startup.initializeIfNeeded(Startup.java:203)
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)
Caused by: com.sun.tools.attach.AttachNotSupportedException: Unable to enqueue operation: the target VM does not support attach mechanism
at sun.tools.attach.WindowsVirtualMachine.(WindowsVirtualMachine.java:58)
at sun.tools.attach.WindowsAttachProvider.attachVirtualMachine(WindowsAttachProvider.java:58)
at com.sun.tools.attach.VirtualMachine.attach(VirtualMachine.java:207)
at mockit.internal.startup.JDK6AgentLoader.attachToThisVM(JDK6AgentLoader.java:110)

Is my javaagent setting correct? If it is, what else could be causing this error?

Michael K
  • 3,297
  • 3
  • 25
  • 37

2 Answers2

0

I don't know if that's the solution, but you are not setting the classpath correctly. Try this:

<classpath>
    <pathelement location="${LIB_PATH}/jmockit.jar" />
    <pathelement location="${LIB_PATH}/junit-4.8.2.jar" />
</classpath>
Sean Patrick Floyd
  • 292,901
  • 67
  • 465
  • 588
0

Your jvmarg to Ant for javaagent looks correct. Are you using Java 6 or a JVM that otherwise supports the Attach API? It looks like you have to also pass -Dcom.sun.management.jmxremote to enable it for older versions of Java. I'm assuming you need it because the exception says "the target VM does not support attach mechanism".

For IBM JDK 6 it appears you need to specify -Dcom.ibm.tools.attach.enable=yes.

WhiteFang34
  • 70,765
  • 18
  • 106
  • 111
  • I tried what you suggested, and it didn't change my output. The strange thing is, I can run the tests with Eclipse's JUnit runner and it works fine with the javaagent. Keeping looking... – Michael K Mar 18 '11 at 18:19
  • The stack trace references `J9VMInternals` which looks like it means you're using the IBM JDK? Perhaps that version doesn't support the Attach API and you're using a different JDK for Eclipse. I recommend the Sun (Oracle) JDK 6 now, it's the fastest with the latest updates. – WhiteFang34 Mar 18 '11 at 18:22
  • The Eclipse I have is technically IBM's Rational Application Developer :) I don't know if it supports the Attach API, but I have to use the javaagent in my JUnit run config so probably not. This IBM JDK is *supposedly* Java 6 compatible. Unfortunately switching isn't an option for business reasons. – Michael K Mar 18 '11 at 18:26
  • It looks like you might need `-Dcom.ibm.tools.attach.enable=yes` to enable the Attach API for IBM JDK 6. – WhiteFang34 Mar 18 '11 at 18:34
  • I ran ant with -v and got "Could not load definitions from resource org/apache/tools/ant/antlib.xml". Could that have anything to do with it? I now get no junitreport output, but that seems not to be related as nothing happens with orig code or changed. – Michael K Mar 18 '11 at 19:03
  • @WhiteFang34 i have added but at at runtime it shows issue.. – spandey Sep 08 '20 at 08:11