2013-05-10 110 views
1

在我当前的项目中,我正在使用junit测试。在我的本地电脑上运行我的ant文件会生成我的测试报告,但是当竹子试图运行我的测试时,它会生成以下输出。运行junit任务时,竹蚂蚁任务失败

我的错误是什么?

SimplerTest.java

import static org.junit.Assert.*; 

import org.junit.Test; 

public class SimplerTest { 


@Test 
public void dummerTest() 
{ 
    assertTrue(true); 
} 
} 

本地输出:

Buildfile: C:\Users\jussi\git\kingdom-builder-repository\build.xml 

compile-test: 
[javac] Compiling 1 source file to C:\Users\jussi\git\kingdom-builder-repository\bin 

junit: 
    [junit] Running me.jussi.kingdombuilder.SimplerTest 
    [junit] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0,062 sec 

main: 

BUILD SUCCESSFUL 
Total time: 1 second 

服务器输出:

compile-test: 
[javac] Compiling 1 source file to /var/atlassian/application-data/bamboo/xml-data/build-dir/KB-KBP1-JOB1/bin 

junit: 

BUILD FAILED 
/var/atlassian/application-data/bamboo/xml-data/build-dir/KB-KBP1-JOB1/build.xml:108: Using loader AntClassLoader[/opt/apache-ant-1.9.0/lib/ant-launcher.jar:/opt/ant/lib/ant.jar:/opt/ant/lib/ant-junit.jar:/opt/ant/lib/ant-junit4.jar:/var/atlassian/application-data/bamboo/xml-data/build-dir/KB-KBP1-JOB1/kingdom-builder/libs/junit-4.10.jar:/var/atlassian/application-data/bamboo/xml-data/build-dir/KB-KBP1-JOB1/bin] 
on class org.apache.tools.ant.taskdefs.optional.junit.XMLJUnitResultFormatter: java.lang.NoClassDefFoundError: junit/framework/TestListener 

的build.xml

<?xml version="1.0"?> 
<project name="KingdomBuild" default="main" basedir="."> 
<!-- Sets variables which can later be used. --> 
<!-- The value of a property is accessed via ${} --> 

<property name="test.src.dir" location="kingdom-builder/test" /> 
<property name="build.dir" location="bin" /> 
<property name="test.report.dir" location="testreport" /> 

<!-- Define the classpath which includes the junit.jar and the classes after compiling--> 
<path id="junit.class.path"> 
    <pathelement location="kingdom-builder/libs/junit-4.10.jar" /> 
    <pathelement location="${build.dir}" /> 
</path> 

<!-- Compiles the java code (including the usage of library for JUnit --> 
<target name="compile-test"> 
    <javac srcdir="${test.src.dir}" destdir="${build.dir}" includeantruntime="false"> 
     <classpath refid="junit.class.path" /> 
    </javac> 
</target> 


<!-- Run the JUnit Tests --> 
<!-- Output is XML, could also be plain--> 
<target name="junit" depends="compile-test"> 
    <junit printsummary="on" fork="true" haltonfailure="true"> 
     <classpath refid="junit.class.path" /> 
     <formatter type="xml" /> 
     <batchtest todir="${test.report.dir}"> 
      <fileset dir="${build.dir}"> 
       <include name="**/*Test*.class" /> 
      </fileset> 
     </batchtest> 
    </junit> 
</target> 

<target name="main" depends="junit"> 
    <description>Main target</description> 
</target> 
</project> 

蚂蚁-v输出:

http://nopaste.info/1abdd27a8e.html

+0

这是Bamboo服务器的所有日志记录吗? 'NoClassDefFoundError'主要是因为没有找到其他类或者无法运行其静态代码,所以在日志中出现的错误或以前的异常的实际堆栈跟踪会帮助很多。你能以某种方式运行详细日志记录的蚂蚁生成? – JBert 2013-05-10 13:54:23

+0

我在问题的末尾添加了ant -v的输出。 – jussi 2013-05-10 14:54:25

回答

4

感谢您的冗长的Ant输出。

看起来您正在Bamboo服务器上运行Ant 1.9.0。有一个已知的问题在Ant bug跟踪系统,这是上发起一个类似的问题的海报SO(错误54835 - Classpath use seems to be broken in junit ant task?):“Class not found with Ant, Ivy and JUnit - error in build.xml?”:

BUILD FAILED 
/home/andrew/project/guice/hg/build.xml:33: java.lang.NoClassDefFoundError: junit/framework/TestListener 
     at java.lang.ClassLoader.defineClass1(Native Method) 
     at java.lang.ClassLoader.defineClass(ClassLoader.java:791) 
... 
     at org.apache.tools.ant.launch.Launcher.run(Launcher.java:280) 
     at org.apache.tools.ant.launch.Launcher.main(Launcher.java:109) 
Caused by: java.lang.ClassNotFoundException: junit.framework.TestListener 
     at java.net.URLClassLoader$1.run(URLClassLoader.java:366) 
     at java.net.URLClassLoader$1.run(URLClassLoader.java:355) 
... 

这个问题并没有一个明确的/简答题,但bug报告做:

它看起来像类搜索被委托给系统的 类加载器,而不是由Ant的类加载器被处理(在 系统类加载器没有的JUnit knoweldge因为JUnit是不在 核心Ant类路径上,但被添加到JUnit任务中由艾维)。假设有一些 JUnit类必须已经加载到这个点,那么Ant类加载器可以看到由Ivy加载的JUnit jar,但是在尝试使用 加载由JUnit Runner使用的类时,Split Classloader似乎不正确地委托。

换句话说:Ant的JUnit任务有错误,并且无法正常工作,我认为您受此特定错误的影响。错误报告的推移并列出这些修复/解决方法:

  • 等待蚂蚁1.9.1(错误报告被标记为固定和释放,可以预计很快)
  • 您的JUnit JAR复制到您的ANTLIB目录并继续使用Ant 1.9.0。如果你想混合使用JUnit版本,这并不是很好,但如果你使用的是4.10左右,它应该可以工作。
  • 使用蚂蚁1.8.4
+0

不错。谢谢。我切换回Ant 1.8.4。 – jussi 2013-05-10 20:42:38