2016-04-30 96 views
0

對Ant很陌生,不知道我在做什麼。我的猜測是我沒有正確設置JUnit類路徑。我發現這個問題有很多類似的問題,儘管沒有任何建議對我有用。請幫忙!JUnit with Ant,另一個java.lang.ClassNotFoundException

我的項目目錄結構:(我懷疑我的目錄結構嚴重搞砸)

MyProject/ 
    bin/main/ 
     project.class 
     test1.class 
     test2.class 
     test3.class 
    build/main/project 
     Project.class 
    build/test/main 
     test1.class 
     test2.class 
     test3.class 
    build/test/project 
     test1.class 
     test2.class 
     test3.class 
    lib/ 
     junit-4.11.jar 
     hamcrest-all-1.3.jar 
    src/main/java/ 
     project.java 
    src/test/java/ 
     test1.java 
     test2.java 
     test3.java 
    build.xml 

我的build.xml

<project name="junit-example"> 
    <property name="main.build.dir" value="build/main"/> 
    <property name="main.src.dir" value="src/main/java"/> 
    <property name="test.build.dir" value="build/test"/> 
    <property name="test.src.dir" value="src/test/java"/> 

    <path id="classpath.test"> 
    <pathelement location="lib/junit-4.11.jar"/> 
    <pathelement location="lib/hamcrest-core-1.3.jar"/> 
    <pathelement location="${main.build.dir}"/> 
    </path> 

    <target name="compile"> 
    <mkdir dir="${main.build.dir}"/> 
    <javac srcdir="${main.src.dir}" destdir="${main.build.dir}" includeantruntime="false"/> 
    </target> 

    <target name="test-compile" depends="compile"> 
    <mkdir dir="${test.build.dir}"/> 
    <javac srcdir="${test.src.dir}" destdir="${test.build.dir}" includeantruntime="false"> 
     <classpath refid="classpath.test"/> 
    </javac> 
    </target> 

    <target name="testAll" depends="test-compile"> 
    <junit printsummary="on" haltonfailure="yes" fork="no"> 
     <classpath> 
      <path refid="classpath.test"/> 
      <pathelement location="${test.build.dir}"/> 
     </classpath> 
     <formatter type="brief" usefile="false" /> 
     <batchtest> 
      <fileset dir="${test.src.dir}" includes="**/*Test.java" /> 
     </batchtest> 
    </junit> 
    </target> 
</project> 

回答

2

您定義batchtest

<batchtest> 
    <fileset dir="${test.src.dir}" includes="**/*Test.java" /> 
</batchtest> 

然而,您在${test.src.dir}中的文件是

src/test/java/ 
    test1.java 
    test2.java 
    test3.java 

嘗試改變batchtest

<batchtest> 
    <fileset dir="${test.src.dir}" includes="**/*test?.java" /> 
</batchtest> 
+0

謝謝!但是,現在它成功建立,但根本不運行測試,你能明白爲什麼嗎? – Katherine

+0

你如何從'ant'執行測試? – rjp