在我目前的项目中,我使用的是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>
ant -v输出:
答案 0 :(得分:4)
感谢详细的Ant输出。
您的Bamboo服务器上似乎正在运行Ant 1.9.0。 Ant bug跟踪器中存在一个已知问题(bug 54835 - Classpath use seems to be broken in junit ant task?),该问题由SO上的类似问题的海报启动:“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)
...
这个问题没有明确/简短的答案,但错误报告确实:
看起来类搜索被委托给系统 类加载器而不是由Ant类加载器处理( 系统类加载器没有JUnit的知识,因为JUnit不在 核心Ant类路径,但由Ivy添加到JUnit任务中。鉴于一些 必须已加载JUnit类才能达到此目的 一点,Ant Classloader可以看到Ivy加载的JUnit jar,但是 在尝试时,Split Classloader似乎被错误地委派了 加载JUnit Runner使用的类。
换句话说:Ant的JUnit任务有bug并且不起作用,我认为你受到这个特定bug的影响。错误报告继续并列出了这些修复/解决方法:
ANTLIB
目录并继续使用Ant 1.9.0。如果你想混合JUnit版本,这不是很好,但如果你使用的只是4.10左右它应该可以工作。