JUnit AntClassNotFoundException
I'm getting ClassNotFoundException when running "ant test" target. All path and libraries exists including junit-4.8.1.jar located in lib folder.
<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>
&开发者_如何学JAVAlt;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>
Console Output:
test:
[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]
I think the problem is that you are pointing junit to the source files and not the complied class files. Also, the fileset
is including *Test*.java
when it should be including *Test*.class
Try replacing your batchtest element with this:
<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>
精彩评论