我正在使用Eclipse Indigo。不知道發生了什麼,但是我試圖從ant構建中調用測試套件,我得到了一個ClassNotFoundException異常。但是,如果我右鍵單擊JUnit Test類並以Junit Test身份運行,它將正常運行測試。該錯誤表示找不到文件./test/_ObservableSortUnitTests。即使給出文件的完整路徑,它也會給出相同的錯誤。JUnit測試套件和Ant構建「ClassNotFoundException」
這是我的錯誤:
Buildfile: /home/jason/Dev/ObservableSort/build.xml
Compile:
Test:
[junit] Testsuite: ./test/_ObservableSortUnitTests
[junit] Tests run: 1, Failures: 0, Errors: 1, Time elapsed: 0 sec
[junit] Caused an ERROR
[junit] ./test/_ObservableSortUnitTests
[junit] java.lang.ClassNotFoundException: ./test/_ObservableSortUnitTests
[junit] at java.lang.Class.forName0(Native Method)
[junit] at java.lang.Class.forName(Class.java:264)
[junit] at org.eclipse.ant.internal.launching.remote.EclipseDefaultExecutor.executeTargets(EclipseDefaultExecutor.java:32)
[junit] at org.eclipse.ant.internal.launching.remote.InternalAntRunner.run(InternalAntRunner.java:424)
[junit] at org.eclipse.ant.internal.launching.remote.InternalAntRunner.main(InternalAntRunner.java:138)
BUILD FAILED
/home/jason/Dev/ObservableSort/build.xml:75: Test ./test/_ObservableSortUnitTests failed
Total time: 1 second
這裏是我的ant腳本(聲明:我是很新的螞蟻):
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<project default="Build" name="CS 151 Project Build Script" >
<!--ANT 1.7 is required -->
<property name="home" value="." />
<property name="src.dir" value = "${home}/src" />
<property name="dest.dir" value="${home}/Release" />
<property name="dir.build" value="${home}/lib" />
<property name="dir.javadoc" value="${dest.dir}/Javadoc" />
<property name="dir.classes" value="${dest.dir}/Classes" />
<property name="dir.junit.reports" value="${dest.dir}/Reports" />
<property name="test.suite.dir" value="${home}/test" />
<property name="test.suite.class" value ="${test.suite.dir}/_ObservableSortUnitTests" />
<path id="build.class.path">
<fileset dir="${dir.build}">
<include name="*.jar" />
</fileset>
</path>
<path id="test.class.path">
<pathelement location="${junit.test.suite}" />
</path>
<target name="Clean" description="Deletes all old files">
<delete dir="${dir.javadoc}" />
<delete dir="${dir.classes}" />
<delete dir="${dir.junit.reports}" />
</target>
<target name="Prepare" description="Creates all necessary directories">
<mkdir dir="${dir.javadoc}" />
<mkdir dir="${dir.classes}" />
<mkdir dir="${dir.junit.reports}" />
</target>
<target name="Compile">
<javac srcdir="${src.dir}" destdir="${dir.classes}" includeantruntime="true">
<classpath refid="build.class.path" />
</javac>
</target>
<target name="Full" description="Executes all build targets">
<antcall target="Clean" />
<antcall target="Prepare" />
<antcall target="Compile" />
<antcall target="Test" />
<antcall target="Build" />
<antcall target="Javadoc" />
<antcall target="run" />
</target>
<target name="Build" description="Creates executable jar" depends="Clean, Prepare, Compile">
<jar destfile="${dest.dir}/ObservableSort.jar" filesetmanifest="mergewithoutmain">
<manifest>
<attribute name="Main-Class" value="cs151.project1.ObservableSortTest"/>
<attribute name="Class-Path" value="."/>
</manifest>
<fileset dir="${home}/bin"/>
</jar>
</target>
<target name="Run" depends="Compile, Build">
<java jar="${dest.dir}/ObservableSort.jar" fork="true" />
</target>
<target name="Run with Unit Tests" depends="Compile, Build, Test">
<java jar="${dest.dir}/ObservableSort.jar" fork="true" />
</target>
<target name="Javadoc" description="Generate Javadoc" depends="Compile" >
<javadoc access="public" author="true" destdir="${dir.javadoc}" nodeprecated="false" nodeprecatedlist="false" noindex="false" nonavbar="false" notree="false" packagenames="cs151.project1.sorters.insertionsort,cs151.project1.sorters.selectionsort,cs151.project1.Quantifiable,cs151.project1,cs151.project1.sorters.quicksort,cs151.project1.views" source="1.6" sourcepath="${src.dir}" splitindex="false" use="true" version="true"/>
</target>
<target name="Test" depends="Compile">
<junit>
<classpath refid="build.class.path" />
<classpath refid="test.class.path" />
<formatter type="plain" usefile="false" />
<test name="${test.suite.class}" haltonerror="true" />
</junit>
</target>
</project>
請接受[回答](http://meta.stackexchange.com/questions/5234)以前的一些問題。 – oers