2011-08-08 12 views
6

Eclipseで正しく動作するプロジェクトでJUnitテストを行っています。ClassNotFoundExceptionでAnt Failedと統合されたJunitテスト

これで、これらのテストをantタスクと統合しようとしました。私は、次のantスクリプトを作ることを確認するには:

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

をディレクトリ$ {libtest.homは} JUnitの-4.8.1.jarとhamcrestコア-1.1.jarが含まれています。私は、次のコマンドを起動すると

:アリユニットテストを、MyTestというの実行は次の出力で失敗します。

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] 

com.test.MyTestがクラスパスでもあるので、それは非常に奇妙だために指摘アリスクリプトの私の仕事junit。誰かがこの問題を解決するアイデアを持っていますか?

ありがとうございました。

シルバイン。

答えて

3

${testclasses.home}ディレクトリは、<junit>タスクのクラスパス上にありません。

これは、com.test.MyTestのクラスファイルが存在する場所だと思います。あなたの答えのための

<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> 
+0

ありがとう:ここ

は、ユニットテストの対象に変更されます。それは私の問題を解決する。私は、私のjunit antタスクのクラスパスにテストクラスを入れるのを忘れました。 – sylsau

+0

私の場合、このソリューションは別のエラーになります:java.util.zip.ZipException;こちらをご覧ください:http://stackoverflow.com/questions/8655193/not-able-to-run-test-through-ant Mayoaresの回答です。上記のタグに変更することで、このエラーを修正することができました。 – srnka

関連する問題