我是 Ant 的初学者,但在尝试使用 JUnit 时遇到问题:
<target name="test" depends="compile" description="JUnit Tests">
<junit printsummary="yes" failureproperty="junit.failure" fork="yes">
<classpath refid="junit.classpath"/>
<classpath path="${myAntTestBin.dir}"/>
<formatter type="plain" usefile="false"/>
<test name="fr.isima.myAntTest.ProjectTest"/>
</junit>
</target>
但是现在,我遇到了这些错误:
test:
[junit] Running fr.isima.myAntTest.ProjectTest
[junit] Testsuite: fr.isima.myAntTest.ProjectTest
[junit] Tests run: 1, Failures: 0, Errors: 1, Time elapsed: 0 sec
[junit] Tests run: 1, Failures: 0, Errors: 1, Time elapsed: 0 sec
[junit]
[junit] Caused an ERROR
[junit] fr.isima.myAntTest.ProjectTest
[junit] java.lang.ClassNotFoundException: fr.isima.myAntTest.ProjectTest
[junit] at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
[junit] at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
[junit] at java.security.AccessController.doPrivileged(Native Method)
[junit] at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
[junit] at java.lang.ClassLoader.loadClass(ClassLoader.java:423)
[junit] at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
[junit] at java.lang.ClassLoader.loadClass(ClassLoader.java:356)
[junit] at java.lang.Class.forName0(Native Method)
[junit] at java.lang.Class.forName(Class.java:186)
[junit]
[junit] Test fr.isima.myAntTest.ProjectTest FAILED
问题是什么? 预先感谢您!
最佳答案
我怀疑这就是问题所在:
<test name="${myAntTestBin}/ProjectTest.class"/>
它正在寻找类的名称,而不是位置:
<test name="ProjectTest"/>
...如果合适的话,用一个包名称来限定它(当然,你确实应该有)。例如:
<test name="foo.bar.ProjectTest" />
而且还有,正如 JB Nizet 提到的,这个:
<classpath path="${myAntTestBin}"/>
应该是:
<classpath path="${myAntTestBin.dir}"/>
匹配您的声明:
<property name="myAntTestBin.dir" value="../myAntTest/bin/fr/isima/myAntTest"/>
编辑:现在您已经(完全)编辑了问题,看起来您的类路径错误了,实际上应该是:
<property name="myAntTestBin.dir" value="../myAntTest/bin"/>
类路径应该是包含类的目录结构的基。这就像在普通 Java 中运行时一样:
java -cp bin foo.bar.Baz
如果您有包含 Baz.class
的 bin/foo/bar
目录结构。
关于Java Ant错误: ClassNotFoundException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15177767/