JUnit Test Suite和Ant构建“ClassNotFoundException”

时间:2011-11-14 10:52:25

标签: java eclipse ant junit

我正在使用Eclipse Indigo。不知道发生了什么,但是我尝试从ant构建中调用测试套件我得到了ClassNotFoundException。但是,如果我右键单击JUnit Test类并运行Junit Test,它会运行测试。该错误表示找不到文件./test/_ObservableSortUnitTests。即使我给出了文件的完整路径,它也会出现相同的错误。

这是我的错误:

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

这是我的蚂蚁脚本(免责声明:我对蚂蚁很新):

    <?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>

3 个答案:

答案 0 :(得分:2)

您正在使用testsuite类的路径,而不是类的完全限定名称。

即使java类存储在文件系统文件夹中,Java也要求您指定软件包名称,而不是斜线。

正确的类名很可能是test._ObservableSortUnitTests_ObservableSortUnitTests

答案 1 :(得分:2)

如果在junit任务中指定测试类的名称,则需要使用完全限定的类名,如Mark所说。来自JUnit task

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

但是,如果您正在使用批处理测试,则可以指定文件名,但需要在末尾添加.java:

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

答案 2 :(得分:0)

愚蠢的错误:JUnit类尚未编译。

解决方案:

  • 将项目编译到目录X
  • 将JUnit类编译到目录Y
  • 将JUnit jar添加到目录Z

&lt;&lt;测试&gt;标记,将目录X,Y和Z添加到类路径。