我有两个相同的构建文件,看起来像这样
build . xml
<?xml version="1.0" encoding="UTF-8"?>
<project name="Tryout" default="test1" basedir=".">
<property name="src" location="src"/>
<property name="build" location="bin"/>
<property name="junit" location="junit-4.10.jar"/>
<target name="compile" description="compile the source" >
<javac srcdir="${src}" destdir="${build}" debug="on">
<classpath location="junit-4.10.jar"/>
<classpath location="${build}"/>
</javac>
</target>
<target name="test1" depends="compile">
<junit printsummary="yes" fork="no" haltonfailure="no">
<classpath location="${build}"/>
<classpath location="junit-4.10.jar"/>
<formatter type="plain"/>
<test name="TestOne"/>
<test name="TestTwo"/>
</junit>
</target>
</project>
唯一的区别是在Trial1中,类在bin
中,而在Trial2中,类在bin/one/two/three
中,所有的类都在one.two.three
包中。Trial1工作良好,运行junit测试并将结果打印到txt文件中。Trial2能够将源代码编译到适当的文件夹中,但是在执行junit时失败。下面是错误:
Testsuite: TestOne
Tests run: 1, Failures: 0, Errors: 1, Time elapsed: 0 sec
Caused an ERROR
TestOne
java.lang.ClassNotFoundException: TestOne
at java.lang.ClassLoader.loadClass(ClassLoader.java:356)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:264)
这是我认为与调试相关的部分
Class java.util.ArrayList loaded from parent loader (parentFirst)
Finding class org.apache.tools.ant.taskdefs.optional.junit.JUnitTestRunner$3
Loaded from /usr/share/ant/lib/ant-junit.jar org/apache/tools/ant/taskdefs/optional/junit/JUnitTestRunner$3.class
Finding class TestOne
Class java.lang.System loaded from parent loader (parentFirst)
Class java.lang.StringBuffer loaded from parent loader (parentFirst)
[junit] Running TestOne
Class org.apache.tools.ant.util.StringUtils loaded from parent loader (parentFirst)
Class org.apache.tools.ant.util.FileUtils loaded from parent loader (parentFirst)
[junit] Tests run: 1, Failures: 0, Errors: 1, Time elapsed: 0 sec
[junit] Test TestOne FAILED
任何帮助指给我一个正确的方向将非常感激!!感谢您的宝贵时间。
应该是<test name="one.two.three.TestOne"/>
,因为您的测试类名称是one.two.three.TestOne
。
如果您想让Ant找到测试所在的位置,使用如下代码:
<batchtest>
<fileset dir="${src}">
<include name="**/Test*.java"/>
</fileset>
</batchtest>