通过ant脚本运行JMockit测试时抛出异常

时间:2013-05-31 10:06:31

标签: ant jmockit

我正在尝试使用ant脚本运行JMcokit测试。但我得到例外。我试图在其他帖子中提到使用'jvmargs'包含java代理,也尝试在类路径中的junit jar之前添加jmockit jar,但它没用。 如果我在我的eclipse中添加java代理参数,那么我可以通过eclipse运行它们,但不能通过ant脚本运行它们。 我能够通过ant脚本运行它们的唯一方法是在我的java路径中添加attach.dll。但这不是一个可行的解决方案,需要解决。

Ant脚本:

    <!-- Run the TestSuite -->
            <junit fork="yes" description="Runs the Junit testcase." printsummary="yes"
                haltonfailure="${halt.on.junit.failure}" showoutput="yes">

                 <sysproperty key="java.library.path" path="C:/Program Files/Java/jdk1.6.0_12/jre/bin/attach.dll"/>  

                <jvmarg value="-javaagent:${lib.dir}/jmockit.jar" />

                <jvmarg value=" -Dcom.sun.management.jmxremote"/>


            <!--    <classpath path="${lib.dir}/jmockit-0.999.8.jar" />
                <classpath path="${lib.dir}/junit-4.7.jar" />-->


                <classpath>
                    <pathelement location="${lib.dir}/jmockit-0.999.8.jar" />
                    <pathelement location="${lib.dir}/junit-4.7.jar" />
                    <pathelement location="${lib.dir}/activemq-all-5.5.1.jar" />
                    <pathelement location="${classes.dir}" />
                    <!--<fileset dir="${lib.dir}">
                        <include name="**/*.jar" />
                        <exclude name="**/jmockit-0.999.8.jar, **/junit-4.7.jar" />
                    </fileset>-->
                </classpath>


                <!-- <test name="com.quest.shareplex.hadoopconnector.junits.AllTests" 
                    fork="no" todir="${junit.output.dir}"> <formatter type="xml" /> </test> -->

                <batchtest fork="no" todir="${junit.output.dir}">
                    <formatter type="xml" />
                    <fileset dir="${connector.src}">
                        <include name="**/*Test.java" />
                        <exclude name="**/*HDFSUtilsTest*.java" />
                    </fileset>
                </batchtest>

            </junit>

请告诉我,如果有其他方法可以使用,或者我做错了什么。 我已经尝试了堆栈溢出时提到的所有方法: - 想改变java lib路径 - 要改变java类路径 - 增加了java代理 但没有任何效果。

例外:

<error type="java.lang.reflect.InvocationTargetException">java.lang.reflect.InvocationTargetException
    at java.lang.reflect.Constructor.newInstance(Unknown Source)
    at org.eclipse.ant.internal.launching.remote.EclipseSingleCheckExecutor.executeTargets(EclipseSingleCheckExecutor.java:30)
    at org.eclipse.ant.internal.launching.remote.EclipseSingleCheckExecutor.executeTargets(EclipseSingleCheckExecutor.java:30)
    at org.eclipse.ant.internal.launching.remote.EclipseDefaultExecutor.executeTargets(EclipseDefaultExecutor.java:32)
    at org.eclipse.ant.internal.launching.remote.InternalAntRunner.run(InternalAntRunner.java:424)
    at org.eclipse.ant.internal.launching.remote.InternalAntRunner.main(InternalAntRunner.java:138)
</error>
  <system-out><![CDATA[]]></system-out>
  <system-err><![CDATA[java.lang.UnsatisfiedLinkError: no attach in java.library.path
java.lang.IllegalStateException: Unable to load Java agent; please add lib/tools.jar from your JDK to the classpath
    at mockit.internal.startup.JDK6AgentLoader.getVirtualMachineImplementationFromEmbeddedOnes(JDK6AgentLoader.java:82)
    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:203)
    at org.junit.runner.Runner.<clinit>(Runner.java:25)
    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 junit.framework.JUnit4TestAdapter.<init>(JUnit4TestAdapter.java:31)
    at junit.framework.JUnit4TestAdapter.<init>(JUnit4TestAdapter.java:24)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
    at java.lang.reflect.Constructor.newInstance(Unknown Source)
    at org.apache.tools.ant.taskdefs.optional.junit.JUnitTestRunner.run(JUnitTestRunner.java:473)
    at org.apache.tools.ant.taskdefs.optional.junit.JUnitTask.executeInVM(JUnitTask.java:1420)
    at org.apache.tools.ant.taskdefs.optional.junit.JUnitTask.execute(JUnitTask.java:848)
    at org.apache.tools.ant.taskdefs.optional.junit.JUnitTask.executeOrQueue(JUnitTask.java:1899)
    at org.apache.tools.ant.taskdefs.optional.junit.JUnitTask.execute(JUnitTask.java:800)
    at org.apache.tools.ant.UnknownElement.execute(UnknownElement.java:291)
    at sun.reflect.GeneratedMethodAccessor4.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.apache.tools.ant.dispatch.DispatchUtils.execute(DispatchUtils.java:106)
    at org.apache.tools.ant.Task.perform(Task.java:348)
    at org.apache.tools.ant.Target.execute(Target.java:390)
    at org.apache.tools.ant.Target.performTasks(Target.java:411)
    at org.apache.tools.ant.Project.executeSortedTargets(Project.java:1399)
    at org.apache.tools.ant.helper.SingleCheckExecutor.executeTargets(SingleCheckExecutor.java:38)
    at org.eclipse.ant.internal.launching.remote.EclipseSingleCheckExecutor.executeTargets(EclipseSingleCheckExecutor.java:30)
    at org.apache.tools.ant.Project.executeTargets(Project.java:1251)
    at org.apache.tools.ant.taskdefs.Ant.execute(Ant.java:442)
    at org.apache.tools.ant.UnknownElement.execute(UnknownElement.java:291)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.apache.tools.ant.dispatch.DispatchUtils.execute(DispatchUtils.java:106)
    at org.apache.tools.ant.Task.perform(Task.java:348)
    at org.apache.tools.ant.Target.execute(Target.java:390)
    at org.apache.tools.ant.Target.performTasks(Target.java:411)
    at org.apache.tools.ant.Project.executeSortedTargets(Project.java:1399)
    at org.apache.tools.ant.helper.SingleCheckExecutor.executeTargets(SingleCheckExecutor.java:38)
    at org.eclipse.ant.internal.launching.remote.EclipseSingleCheckExecutor.executeTargets(EclipseSingleCheckExecutor.java:30)
    at org.apache.tools.ant.Project.executeTargets(Project.java:1251)
    at org.apache.tools.ant.taskdefs.Ant.execute(Ant.java:442)
    at org.apache.tools.ant.UnknownElement.execute(UnknownElement.java:291)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.apache.tools.ant.dispatch.DispatchUtils.execute(DispatchUtils.java:106)
    at org.apache.tools.ant.Task.perform(Task.java:348)
    at org.apache.tools.ant.Target.execute(Target.java:390)
    at org.apache.tools.ant.Target.performTasks(Target.java:411)
    at org.apache.tools.ant.Project.executeSortedTargets(Project.java:1399)
    at org.apache.tools.ant.Project.executeTarget(Project.java:1368)
    at org.apache.tools.ant.helper.DefaultExecutor.executeTargets(DefaultExecutor.java:41)
    at org.eclipse.ant.internal.launching.remote.EclipseDefaultExecutor.executeTargets(EclipseDefaultExecutor.java:32)
    at org.apache.tools.ant.Project.executeTargets(Project.java:1251)
    at org.eclipse.ant.internal.launching.remote.InternalAntRunner.run(InternalAntRunner.java:424)
    at org.eclipse.ant.internal.launching.remote.InternalAntRunner.main(InternalAntRunner.java:138)

谢谢, Mayank

2 个答案:

答案 0 :(得分:2)

看起来你的java是普通的jre。

  

java.library.path java.lang.IllegalStateException:无法加载Java   剂;请将JDK中的lib / tools.jar添加到类路径中       at mockit.internal.startup.JDK6AgentLoader.getVirtualMachineImplementationFromEmbeddedOnes(JDK6AgentLoader.java:82)       at mockit.internal.startup.JDK6AgentLoader.loadAgent(JDK6AgentLoader.java:54

Jmockit需要 JDK 才能工作。

答案 1 :(得分:2)

如果您使用的是Mac,则会发生这种情况,因为分叉的JVM在类路径中没有tools.jar。我今天遇到了这个blogged about it