2010-12-11 139 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