我的Java JUnit测试位于结构:src/com/project/test/test_junit.java
我的蚂蚁脚本是:
<project name="Test" basedir="." default="unit-test">
<property name="src" value="src"/>
<property name="build-test-classes" value="${src}/com/project/test"/>
<property name="lib" value="lib"/>
<property name="junit4" value="lib/junit-4.12.jar"/>
<target name="unit-test">
<junit printsummary="yes" haltonfailure="yes">
<formatter type="plain" usefile="false"/>
<classpath>
<pathelement location="${junit4}"/>
<pathelement location="${build-test-classes}"/>
</classpath>
<batchtest>
<fileset dir="${build-test-classes}">
<include name="**/*.java"/>
</fileset>
</batchtest>
</junit>
</target>
</project>
PS:我更改了一些名称以制作不太具体的代码。
在运行Ant脚本时,我遇到以下问题:
unit-test:
[junit] Running test_junit
[junit] Testsuite: test_junit
[junit] Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 0 sec
[junit] Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 0 sec
[junit]
[junit] Caused an ERROR
[junit] test_junit
[junit] java.lang.ClassNotFoundException: test_junit
[junit] at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
[junit] at java.lang.Class.forName0(Native Method)
[junit] at java.lang.Class.forName(Class.java:348)
[junit]
BUILD FAILED
/build_junit_test.xml:23: Test AccountGS_Junit failed
at org.apache.tools.ant.taskdefs.optional.junit.JUnitTask.actOnTestResult(JUnitTask.java:1959)
at org.apache.tools.ant.taskdefs.optional.junit.JUnitTask.execute(JUnitTask.java:858)
at org.apache.tools.ant.taskdefs.optional.junit.JUnitTask.executeOrQueue(JUnitTask.java:1904)
at org.apache.tools.ant.taskdefs.optional.junit.JUnitTask.execute(JUnitTask.java:804)
at org.apache.tools.ant.UnknownElement.execute(UnknownElement.java:292)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
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:435)
at org.apache.tools.ant.Target.performTasks(Target.java:456)
at org.apache.tools.ant.Project.executeSortedTargets(Project.java:1393)
at org.apache.tools.ant.Project.executeTarget(Project.java:1364)
at org.apache.tools.ant.helper.DefaultExecutor.executeTargets(DefaultExecutor.java:41)
at org.apache.tools.ant.Project.executeTargets(Project.java:1248)
at org.apache.tools.ant.Main.runBuild(Main.java:851)
at org.apache.tools.ant.Main.startAnt(Main.java:235)
at org.apache.tools.ant.launch.Launcher.run(Launcher.java:280)
at org.apache.tools.ant.launch.Launcher.main(Launcher.java:109)
Total time: 0 seconds
我已经在这里看了很多问题,但似乎任何答案都解决了我的问题。
答案 0 :(得分:2)
我敢打赌你还没有编译测试代码。提示是build-test-classes属性,它指向源代码树中的文件:
<property name="src" value="src"/>
<property name="build-test-classes" value="${src}/com/project/test"/>
创建一个编译源代码的目标
<target name="compile" depends="resolve,resources" description="Compile code">
<mkdir dir="${build.dir}/classes"/>
<javac srcdir="${src.dir}" destdir="${build.dir}/classes" includeantruntime="false" debug="true" classpathref="compile.path"/>
</target>
创建一个编译测试代码的目标
<target name="compile-tests" depends="compile" description="Compile tests">
<mkdir dir="${build.dir}/test-classes"/>
<javac srcdir="${test.src.dir}" destdir="${build.dir}/test-classes" includeantruntime="false" debug="true">
<classpath>
<path refid="test.path"/>
<pathelement path="${build.dir}/classes"/>
</classpath>
</javac>
</target>
然后Junit应该工作
<target name="test" depends="compile-tests" description="Run unit tests">
<mkdir dir="${build.dir}/test-reports"/>
<junit printsummary="yes" haltonfailure="yes">
<classpath>
<path refid="test.path"/>
<pathelement path="${build.dir}/classes"/>
<pathelement path="${build.dir}/test-classes"/>
</classpath>
<formatter type="xml"/>
<batchtest fork="yes" todir="${build.dir}/test-reports">
<fileset dir="${test.src.dir}">
<include name="**/*Test*.java"/>
<exclude name="**/AllTests.java"/>
</fileset>
</batchtest>
</junit>
</target>