2010-12-11 36 views
2

運行「ant test」目標時出現ClassNotFoundException。 所有路徑和庫存在,包括位於lib文件夾中的junit-4.8.1.jar。JUnit AntClassNotFoundException

<project name="progs" default="test" basedir="."> 

    <!--++++++++++ Properties ++++++++++--> 
    <property name="src-main" location="PC_Serie_3"/> 
    <property name="src-test" location="PC_Serie_3_Tests"/> 
    <property name="target" location="target"/> 
    <property name="target-classes" location="target/PC_Serie_3"/> 
    <property name="target-test-classes" location="target/PC_Serie_3_Tests"/> 
    <property name="target-test-reports" location="target/test-reports"/> 

    <path id="test.extlibs.class.path"> 
    <fileset dir="lib"> 
     <include name="**/*.jar" /> 
    </fileset> 
    </path> 

    <!--++++++++++ Targets ++++++++++--> 
    <target name="init" description ="Creates the target folders"> 
    <mkdir dir="${target-classes}"/> 
    <mkdir dir="${target-test-classes}"/> 
    <mkdir dir="${target-test-reports}"/> 
    </target> 

    <target name="clean" description="Removes the target folders" > 
    <delete includeEmptyDirs="true" failonerror="false" verbose="true" > 
     <fileset dir="${target}" defaultexcludes="false"/> 
    </delete> 
    </target> 

    <target name="compile-main" depends="init" description="Compiles the main source" > 
    <javac debug="true" 
      srcdir="${src-main}" 
      destdir="${target-classes}" 
      includeantruntime="false"> 
    </javac> 
    </target> 

    <target name="compile-test" depends="compile-main" description="Compiles the test source" > 
    <javac debug="true" 
      debugLevel="source" 
      srcdir="${src-test}" 
      destdir="${target-test-classes}" 
      includeantruntime="true"> 
     <classpath> 
     <pathelement location="${target-classes}"/> 
     <path refid="test.extlibs.class.path" /> 
     </classpath> 
    </javac> 
    </target> 

<target name="test" depends="compile-test" description="Runs the tests"> 
    <echo>Running the junit tests...</echo> 
    <junit printsummary="yes" haltonfailure="true" showoutput="true" > 
     <classpath> 
     <pathelement location="${src-main}"/> 
     <pathelement location="${target-classes}"/> 
     <pathelement location="${target-test-classes}"/> 
     <path refid="test.extlibs.class.path" /> 
     </classpath> 

    <batchtest fork="yes" todir="${target-test-reports}" > 
     <fileset dir="${src-test}"> 
      <include name="**/*Test*.java"/> 
     </fileset> 
     <formatter type="xml"/> 
     <formatter type="plain" usefile="false" /> 
     </batchtest> 
     </junit> 
    </target> 

    <target name="package" depends="test" description="Packages the main classes into a jar" > 
    <buildnumber /> 
    <jar jarfile="${target}/library.jar" basedir="${target-classes}"/> 
    </target> 

    <!-- USAGE: ant deploy-app --> 
    <target name="deploy-lib" depends="package"> 
    </target> 
</project> 

控制檯輸出:

測試:

[echo] Running the junit tests... 
    [junit] Running src.DocumentDBTests.DocumentDBv1Test 
    [junit] Testsuite: src.DocumentDBTests.DocumentDBv1Test 
    [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] src.DocumentDBTests.DocumentDBv1Test 
    [junit] java.lang.ClassNotFoundException: src.DocumentDBTests.DocumentDBv1Te 
st 
    [junit]  at java.net.URLClassLoader$1.run(URLClassLoader.java:202) 
    [junit]  at java.security.AccessController.doPrivileged(Native Method) 
    [junit]  at java.net.URLClassLoader.findClass(URLClassLoader.java:190) 
    [junit]  at java.lang.ClassLoader.loadClass(ClassLoader.java:307) 
    [junit]  at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301) 

    [junit]  at java.lang.ClassLoader.loadClass(ClassLoader.java:248) 
    [junit]  at java.lang.Class.forName0(Native Method) 
    [junit]  at java.lang.Class.forName(Class.java:169) 
    [junit] 
+0

我與叉子試=「不」,但問題仍然存在。 – aumanets 2010-12-11 18:56:13

回答

1

我認爲問題是,你是指向JUnit來源文件,而不是遵守類文件。此外,fileset被包括*Test*.java時,應當包括*Test*.class

嘗試用這個替換您batchtest元素:

<batchtest fork="yes" todir="${target-test-reports}" > 
    <fileset dir="${target-test-classes}"> 
     <include name="**/*Test*.class"/> 
    </fileset> 
    <formatter type="xml"/> 
    <formatter type="plain" usefile="false" /> 
</batchtest> 
+0

你說得對。感謝你的幫助,這一直使我瘋狂! – aumanets 2010-12-13 22:08:44