这是来自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到preferences ->Ant->Runtime->Global Entries还有org.hamcrest。*到首选项->JUnit->添加包(但这并不能解决NoClassDefFoundError: org/hamcrest/selfdescription的问题)
如果我直接在eclipse中使用JUnit插件运行UnitTest*.java,它会工作并显示所有通过的测试。
谁能帮我想出解决办法吗?谢谢你。找到问题。这是由于错误的路径位置造成的。我只是将path元素更改到正确的位置并求解它。蚂蚁只需要正确的路径。每次检查位置是否正确。
代替<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"/>