简体   繁体   中英

JUnit Test Suite and Ant Build “ClassNotFoundException”

I am using Eclipse Indigo. Not sure what is going on, but what I try to invoke the test suite from an ant build I get a ClassNotFoundException. However, if I right click on the JUnit Test class and run as Junit Test, it runs the tests fine. The error says that the file ./test/_ObservableSortUnitTests is not found. It gives the same error even if I give a full path to the file.

Here is my error:

Buildfile: /home/jason/Dev/ObservableSort/build.xml
Compile:
Test:
    [junit] Testsuite: ./test/_ObservableSortUnitTests
    [junit] Tests run: 1, Failures: 0, Errors: 1, Time elapsed: 0 sec
    [junit]     Caused an ERROR
    [junit] ./test/_ObservableSortUnitTests
    [junit] java.lang.ClassNotFoundException: ./test/_ObservableSortUnitTests
    [junit]     at java.lang.Class.forName0(Native Method)
    [junit]     at java.lang.Class.forName(Class.java:264)
    [junit]     at org.eclipse.ant.internal.launching.remote.EclipseDefaultExecutor.executeTargets(EclipseDefaultExecutor.java:32)
    [junit]     at org.eclipse.ant.internal.launching.remote.InternalAntRunner.run(InternalAntRunner.java:424)
    [junit]     at org.eclipse.ant.internal.launching.remote.InternalAntRunner.main(InternalAntRunner.java:138)

BUILD FAILED
/home/jason/Dev/ObservableSort/build.xml:75: Test ./test/_ObservableSortUnitTests failed

Total time: 1 second

Here is my ant script (disclaimer: I am very new to ant):

    <?xml version="1.0" encoding="UTF-8" standalone="no"?>
<project default="Build" name="CS 151 Project Build Script" >
    <!--ANT 1.7 is required  -->
    <property name="home" value="." />                
    <property name="src.dir" value = "${home}/src" />
    <property name="dest.dir" value="${home}/Release" />
    <property name="dir.build" value="${home}/lib" />
    <property name="dir.javadoc" value="${dest.dir}/Javadoc" />
    <property name="dir.classes" value="${dest.dir}/Classes" />
    <property name="dir.junit.reports" value="${dest.dir}/Reports" />
    <property name="test.suite.dir" value="${home}/test" />
    <property name="test.suite.class" value ="${test.suite.dir}/_ObservableSortUnitTests" />

    <path id="build.class.path">
        <fileset dir="${dir.build}">
            <include name="*.jar" />
        </fileset>
    </path> 

    <path id="test.class.path">
        <pathelement location="${junit.test.suite}" />
    </path>

    <target name="Clean" description="Deletes all old files">
        <delete dir="${dir.javadoc}" />
        <delete dir="${dir.classes}" />
        <delete dir="${dir.junit.reports}" />
    </target>

    <target name="Prepare" description="Creates all necessary directories">
        <mkdir dir="${dir.javadoc}" />
        <mkdir dir="${dir.classes}" />
        <mkdir dir="${dir.junit.reports}" />
    </target>   

    <target name="Compile">
        <javac srcdir="${src.dir}" destdir="${dir.classes}" includeantruntime="true">
            <classpath refid="build.class.path" />
        </javac>
    </target>

    <target name="Full" description="Executes all build targets">
        <antcall target="Clean" />
        <antcall target="Prepare" />
        <antcall target="Compile" />
        <antcall target="Test" />
        <antcall target="Build" />
        <antcall target="Javadoc" />
        <antcall target="run" />
    </target>

    <target name="Build" description="Creates executable jar" depends="Clean, Prepare, Compile">
        <jar destfile="${dest.dir}/ObservableSort.jar" filesetmanifest="mergewithoutmain">
            <manifest>
                <attribute name="Main-Class" value="cs151.project1.ObservableSortTest"/>
                <attribute name="Class-Path" value="."/>
            </manifest>
            <fileset dir="${home}/bin"/>
        </jar>
    </target>

    <target name="Run" depends="Compile, Build">
        <java jar="${dest.dir}/ObservableSort.jar" fork="true" />
    </target>           

    <target name="Run with Unit Tests" depends="Compile, Build, Test">
        <java jar="${dest.dir}/ObservableSort.jar" fork="true" />
    </target>

    <target name="Javadoc" description="Generate Javadoc" depends="Compile" >
        <javadoc access="public" author="true"  destdir="${dir.javadoc}" nodeprecated="false" nodeprecatedlist="false" noindex="false" nonavbar="false" notree="false" packagenames="cs151.project1.sorters.insertionsort,cs151.project1.sorters.selectionsort,cs151.project1.Quantifiable,cs151.project1,cs151.project1.sorters.quicksort,cs151.project1.views" source="1.6" sourcepath="${src.dir}" splitindex="false" use="true" version="true"/>
    </target>

    <target name="Test" depends="Compile">
        <junit>
            <classpath refid="build.class.path" />
            <classpath refid="test.class.path" />
            <formatter type="plain" usefile="false" />
            <test name="${test.suite.class}" haltonerror="true" />
        </junit>
    </target>
</project>

You are using the path to the testsuite class instead of the fully qualified name of the class.

Even though java classes are stored in filesystem folders, Java requires you to specify the package name dotted, not slashed.

Most likely the correct classname is test._ObservableSortUnitTests or maybe _ObservableSortUnitTests

If you specify the name of the test class in the junit task, then you need to use the fully qualifed class name, as Mark says. From JUnit task

<junit>
  <test name="my.test.TestCase"/>
</junit>

If, however, you're using a batchtest, you can specify file names, but you need to add the .java on the end:

  <batchtest fork="yes" todir="${reports.tests}">
    <fileset dir="${src.tests}">
      <include name="**/*Test*.java"/>
      <exclude name="**/AllTests.java"/>
    </fileset>
  </batchtest>

Stupid mistake: The JUnit classes were not yet compiled.

Solution:

  • Compile the project into a directory X
  • Compile the JUnit classes into a directory Y
  • Add the JUnit jar to directory Z

Inside the < test > tag, add directories X, Y, and Z to the classpath.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM