Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Junit test integrated with Ant Failed with ClassNotFoundException

I have JUnit tests on my project that run correctly with Eclipse.

So, now I try to integrate these tests with an ant task. To make that I make 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 directory ${libtest.hom} contains junit-4.8.1.jar and hamcrest-core-1.1.jar.

When I launch the following command : ant unit-test, the execution of the 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]

It's very strange because com.test.MyTest is well in the classpath pointed for my task junit in ant script. Someone would have and idea to solve this problem ?

Thanks for your help.

Sylvain.

like image 582
sylsau Avatar asked Aug 08 '11 15:08

sylsau


1 Answers

The ${testclasses.home} directory is nowhere on a classpath for <junit> task.

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

Here is modified unit-test target:

<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>
like image 119
Alexander Pogrebnyak Avatar answered Oct 16 '22 12:10

Alexander Pogrebnyak