2011-11-14 149 views
1

我正在使用Eclipse Indigo。不知道發生了什麼,但是我試圖從ant構建中調用測試套件,我得到了一個ClassNotFoundException異常。但是,如果我右鍵單擊JUnit Test類並以Junit Test身份運行,它將正常運行測試。該錯誤表示找不到文件./test/_ObservableSortUnitTests。即使給出文件的完整路徑,它也會給出相同的錯誤。JUnit測試套件和Ant構建「ClassNotFoundException」

這是我的錯誤:

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 

這裏是我的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> 
+1

請接受[回答](http://meta.stackexchange.com/questions/5234)以前的一些問題。 – oers

回答

0

愚蠢的錯誤:JUnit的類尚未編譯。

解決方案:

  • 工程編譯成一個目錄X
  • 編譯JUnit類到一個目錄Ÿ
  • JUnit的JAR添加到目錄ž

裏面的<測試>標記,將目錄X,Y和Z添加到類路徑中。

2

您正在使用的測試套件類,而不是路徑該類的完全限定名稱。

儘管Java類是存儲在文件系統文件夾中,Java要求您指定包名稱點,而不是削減。

最有可能正確的類名是test._ObservableSortUnitTests也許_ObservableSortUnitTests

+0

我已經嘗試了幾種不同的classpath和filename的排列方式,我認爲它們是正確的,但它似乎並不喜歡我。 – jcampos8782

+0

我有定義$ {home}/test的TestSuite類,它包含在(默認包)中。任何關於下一步嘗試的建議? – jcampos8782

+0

如果您的測試位於'$ {home}/test'文件夾中,那麼您應該將其添加到Test目標的類路徑中。如果TestSuite在默認包中,那麼你應該只指定類名'_ObservableSortUnitTests' –

2

如果指定的junit任務測試類的名字,那麼你需要使用完全組隊參加類名,Mark說。從JUnit task

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

但是,如果您使用的是batchtest,你可以指定文件名,但你需要添加在年底的.java:

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

它是一個TestSuite類。我有定義$ {home}/test的TestSuite類,它包含在(默認包)中。我嘗試了許多不同的可能組合,我認爲這些組合是正確的,沒有任何工作。任何關於下一步嘗試的建議? – jcampos8782

相關問題