Junit test integrated with Ant Fails with ClassNotFoundException

I have JUnit tests in my project that work correctly with Eclipse.

So now I am trying to integrate these tests with the ant task. To do this, I will do the following ant script:

<path id="classpath-test"> <pathelement path="." /> <pathelement path="${classes.home}" /> <fileset dir="${lib.home}" includes="*.jar" /> <fileset dir="${libtest.home}" includes="*.jar" /> </path> <target name="compile" ... > // compiles src code of the project <target name="compile-tests" depends="compile"> <javac srcdir="${test.home}" destdir="${testclasses.home}" target="1.5" source="1.5" debug="true" > <classpath refid="classpath-test" /> </javac> <copy todir="${testclasses.home}"> <fileset dir="${test.home}"> <exclude name="**/*.java"/> </fileset> </copy> </target> <target name="unit-test" depends="compile-tests"> <junit printsummary="false" fork="off" haltonfailure="true"> <classpath refid="classpath-test" /> <formatter type="brief" usefile="false" /> <test name="com.test.MyTest" /> <!--<batchtest todir="${reports.dir}" > <fileset dir="${testclasses.home}" > <exclude name="**/AllTests*"/> <include name="**/*Test.class" /> </fileset> </batchtest>--> </junit> </target> 

The $ {libtest.hom} directory contains junit-4.8.1.jar and hamcrest-core-1.1.jar.

When I run the following command: ant unit-test, the execution of MyTest fails with the following output:

 unit-test: [junit] Testsuite: com.test.MyTest [junit] Tests run: 1, Failures: 0, Errors: 1, Time elapsed: 0 sec [junit] [junit] Null Test: Caused an ERROR [junit] com.test.MyTest [junit] java.lang.ClassNotFoundException: com.test.MyTest [junit] at java.lang.ClassLoader.loadClass(ClassLoader.java:248) [junit] at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:316) [junit] at java.lang.Class.forName0(Native Method) [junit] at java.lang.Class.forName(Class.java:247) [junit] [junit] 

This is very strange, because com.test.MyTest is good in the classpath specified for my junit task in ant script. Would anyone have an idea to solve this problem?

Thanks for your help.

Sylvain.

+6
source share
1 answer

There is no class path for the <junit> task in the ${testclasses.home} <junit> .

I think this is where the class file for com.test.MyTest .

The unit test goal has been changed here:

 <target name="unit-test" depends="compile-tests"> <junit printsummary="false" fork="off" haltonfailure="true"> <classpath> <path refid="classpath-test"/> <fileset dir="${testclasses.home}"/> </classpath> <formatter type="brief" usefile="false" /> <test name="com.test.MyTest" /> <!--<batchtest todir="${reports.dir}" > <fileset dir="${testclasses.home}" > <exclude name="**/AllTests*"/> <include name="**/*Test.class" /> </fileset> </batchtest>--> </junit> </target> 
+3
source

Source: https://habr.com/ru/post/894556/


All Articles