2011-08-08 63 views
6

我的项目上有JUnit测试,它可以在Eclipse中正确运行。与Ant集成的Junit测试使用ClassNotFoundException失败

所以,现在我尝试将这些测试与蚂蚁任务集成在一起。为了让我做以下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。

当我启动以下命令:ant单元测试中,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远在classpath中指出的我的任务junit在ant脚本中。有人会想出解决这个问题吗?

感谢您的帮助。

Sylvain。

回答

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

相关问题