Lucas Oliveira
Lucas Oliveira

Reputation: 677

Ant script to run junit test showing java.lang.ClassNotFoundException

My Java JUnit test is on the structure: src/com/project/test/test_junit.java

My ant script is:

<project name="Test" basedir="." default="unit-test">


    <property name="src" value="src"/>
    <property name="build-test-classes" value="${src}/com/project/test"/>
    <property name="lib" value="lib"/>
    <property name="junit4" value="lib/junit-4.12.jar"/>

    <target name="unit-test">

        <junit printsummary="yes" haltonfailure="yes">
            <formatter type="plain" usefile="false"/>
            <classpath>
                <pathelement location="${junit4}"/>
                <pathelement location="${build-test-classes}"/>
            </classpath>
            <batchtest>
                <fileset dir="${build-test-classes}">
                    <include name="**/*.java"/>
                </fileset>
            </batchtest>
        </junit>
    </target>
</project>

PS.: I changed some names to make less specific code.

While running the Ant script, I face the following issue:

unit-test:
    [junit] Running test_junit
    [junit] Testsuite: test_junit
    [junit] Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 0 sec
    [junit] Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 0 sec
    [junit] 
    [junit]     Caused an ERROR
    [junit] test_junit
    [junit] java.lang.ClassNotFoundException: test_junit
    [junit]     at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    [junit]     at java.lang.Class.forName0(Native Method)
    [junit]     at java.lang.Class.forName(Class.java:348)
    [junit] 

BUILD FAILED
/build_junit_test.xml:23: Test AccountGS_Junit failed
    at org.apache.tools.ant.taskdefs.optional.junit.JUnitTask.actOnTestResult(JUnitTask.java:1959)
    at org.apache.tools.ant.taskdefs.optional.junit.JUnitTask.execute(JUnitTask.java:858)
    at org.apache.tools.ant.taskdefs.optional.junit.JUnitTask.executeOrQueue(JUnitTask.java:1904)
    at org.apache.tools.ant.taskdefs.optional.junit.JUnitTask.execute(JUnitTask.java:804)
    at org.apache.tools.ant.UnknownElement.execute(UnknownElement.java:292)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.apache.tools.ant.dispatch.DispatchUtils.execute(DispatchUtils.java:106)
    at org.apache.tools.ant.Task.perform(Task.java:348)
    at org.apache.tools.ant.Target.execute(Target.java:435)
    at org.apache.tools.ant.Target.performTasks(Target.java:456)
    at org.apache.tools.ant.Project.executeSortedTargets(Project.java:1393)
    at org.apache.tools.ant.Project.executeTarget(Project.java:1364)
    at org.apache.tools.ant.helper.DefaultExecutor.executeTargets(DefaultExecutor.java:41)
    at org.apache.tools.ant.Project.executeTargets(Project.java:1248)
    at org.apache.tools.ant.Main.runBuild(Main.java:851)
    at org.apache.tools.ant.Main.startAnt(Main.java:235)
    at org.apache.tools.ant.launch.Launcher.run(Launcher.java:280)
    at org.apache.tools.ant.launch.Launcher.main(Launcher.java:109)

Total time: 0 seconds

I've already followed loads of questions here, but seems that any answer solved my problem.

Upvotes: 0

Views: 505

Answers (1)

Mark O&#39;Connor
Mark O&#39;Connor

Reputation: 77951

I'll bet that you haven't compiled the test code. The hint is the build-test-classes property that points at files in your source code tree:

<property name="src" value="src"/>
<property name="build-test-classes" value="${src}/com/project/test"/>

Example

Create a target that compiles your source code

<target name="compile" depends="resolve,resources" description="Compile code">
    <mkdir dir="${build.dir}/classes"/>
    <javac srcdir="${src.dir}" destdir="${build.dir}/classes" includeantruntime="false" debug="true" classpathref="compile.path"/>
</target>

Create a target that compiles your test code

<target name="compile-tests" depends="compile" description="Compile tests">
    <mkdir dir="${build.dir}/test-classes"/>
    <javac srcdir="${test.src.dir}" destdir="${build.dir}/test-classes" includeantruntime="false" debug="true">
        <classpath>
            <path refid="test.path"/>
            <pathelement path="${build.dir}/classes"/>
        </classpath>
    </javac>
</target>

Then Junit should work

<target name="test" depends="compile-tests" description="Run unit tests">
    <mkdir dir="${build.dir}/test-reports"/>
    <junit printsummary="yes" haltonfailure="yes">
        <classpath>
            <path refid="test.path"/>
            <pathelement path="${build.dir}/classes"/>
            <pathelement path="${build.dir}/test-classes"/>
        </classpath>
        <formatter type="xml"/>
        <batchtest fork="yes" todir="${build.dir}/test-reports">
            <fileset dir="${test.src.dir}">
                <include name="**/*Test*.java"/>
                <exclude name="**/AllTests.java"/>
            </fileset>
        </batchtest>
    </junit>
</target>

Upvotes: 2

Related Questions