使用Ant使用initializationError构建junit测试:java.lang.NoClassDefFoundError:org / hamcrest / SelfDescribing

时间:2014-07-07 21:53:38

标签: java eclipse ant junit build

这是来自junit测试报告的失败追踪:

java.lang.NoClassDefFoundError: org/hamcrest/SelfDescribing
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:800)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:449)
at java.net.URLClassLoader.access$100(URLClassLoader.java:71)
at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
at java.lang.reflect.Constructor.newInstance(Constructor.java:526)

Caused by: java.lang.ClassNotFoundException: org.hamcrest.SelfDescribing
at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:358)

这是我的build.xml:

<project name="Ant-Test-JUnit" default="main">
<description>
        JUnit Test on MyMath
</description>

<property name="src.dir" location="src"/>
<property name="bin.dir" location="bin"/>

<!-- Variables used for JUnit testin -->
<property name="test.dir" location="src" />
<property name="test.report.dir" location="testreport" />

<path id="test.classpath">
    <pathelement location="${bin.dir}" />
    <pathelement location="../../../../plugins/org.junit_4.11.0.v201203080030/junit.jar" />     
    <pathelement location="../../../../plugins/org.hamcrest.core_1.3.0.v201303031735.jar"/>
</path>

<!-- ================================= 
      target: main             
     ================================= -->
<target name="main" depends="compile, junit">
    <echo message="Junit Test :)"/>
</target>

<!-- - - - - - - - - - - - - - - - - - 
      target: compile                      
     - - - - - - - - - - - - - - - - - -->
<target name="compile" depends="clean, makedir">
    <javac includeantruntime="false" srcdir="${src.dir}" destdir="${bin.dir}">
        <classpath refid="test.classpath"/>
    </javac>
</target>

<!-- - - - - - - - - - - - - - - - - - 
              target: clean, makedir                   
     - - - - - - - - - - - - - - - - - -->
<target name="clean">
    <delete dir="${bin}"/>
    <delete dir="${test.report.dir}" />
</target>

<target name="makedir">
    <mkdir dir="${bin.dir}"/>
    <mkdir dir="${test.report.dir}" />
</target>

<!-- - - - - - - - - - - - - - - - - - 
          target: junit                      
     - - - - - - - - - - - - - - - - - -->
<target name="junit" depends="compile">
    <junit printsummary="yes" fork="true" haltonfailure="yes">
        <classpath refid="test.classpath" />
        <formatter type="xml" />
        <batchtest todir="${test.report.dir}">
            <fileset dir="${src.dir}">
              <include name="**/*UnitTest*.java" />
            </fileset>
        </batchtest>
    </junit>
</target>

我已将Junit_4.11 jar添加到Preference-&gt; Ant-&gt; Runtime-&gt;全局条目 并打包org.hamcrest。*到Preference-&gt; JUnit-&gt;添加包(但这并不能解决NoClassDefFoundError的问题:org / hamcrest / SelfDescribing)

如果我直接在eclipse中使用JUnit插件运行UnitTest * .java,它会工作并显示所有传递的测试。

有人能帮我找出解决方案吗?谢谢。

1 个答案:

答案 0 :(得分:0)

发现问题。这是由错误的路径位置引起的。我只是将路径元素更改为正确的位置并解决它。 Ant只需要正确的路径。每次检查位置是否正确。

替换

<pathelement location="../../../../plugins/org.junit_4.11.0.v201203080030/junit.jar" />     
<pathelement location="../../../../plugins/org.hamcrest.core_1.3.0.v201303031735.jar"/>

<pathelement location="/Applications/eclipse/plugins/org.junit_4.11.0.v201303080030/junit.jar"/>
<pathelement location="/Applications/eclipse/plugins/org.hamcrest.core_1.3.0.v201303031735.jar"/>