运行junit任务时Bamboo Ant Task失败 [英] Bamboo Ant Task fails when running junit task
问题描述
在我当前的项目中,我正在使用 junit 测试.在我的本地电脑上运行我的 ant 文件会按预期产生我的测试报告,但是当竹子尝试运行我的测试时,它会产生以下输出.
In my current project I'm using junit tests. Runing my ant file on my local pc produces my Test Report as expected, but when bamboo tries to run my tests, it produces the following output.
我的错误是什么?
SimplerTest.java
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 输出:
http://nopaste.info/1abdd27a8e.html
推荐答案
感谢 Ant 的详细输出.
Thanks for the verbose Ant output.
您似乎在 Bamboo 服务器上运行 Ant 1.9.0.有一个已知问题(bug 54835 - 类路径的使用似乎在 junit ant 中被破坏了任务?)在 Ant 错误跟踪器中,这是由关于 SO 的类似问题的发布者启动的:在 Ant、Ivy 和 JUnit 中找不到类 - build.xml 中的错误?":
It appears you are running Ant 1.9.0 on your Bamboo server. There is a known issue (bug 54835 - Classpath use seems to be broken in junit ant task?) in the Ant bug tracker, which was started by the poster of a similar question on 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)
...
那个问题没有明确/简短的答案,但错误报告有:
That question didn't have a clear / short answer, but the bug report does:
看起来类搜索被委托给系统的类加载器,而不是由 Ant 类加载器(系统类加载器对 JUnit 一无所知,因为 JUnit 不在核心 Ant 类路径,但被 Ivy 添加到 JUnit 任务中).鉴于一些JUnit 类必须已经加载才能达到这个点,Ant Classloader 可以看到 Ivy 加载的 JUnit jars,但是尝试时,拆分类加载器似乎委派不正确加载 JUnit Runner 使用的类.
It looks like the class search is being delegated to the System's classloader rather than being handled by the Ant Classloader (the system Classloader has no knoweldge of JUnit since JUnit isn't on the core Ant Classpath, but is added to the JUnit task by Ivy). Given some JUnit classes must have already been loaded to have reached this point, the Ant Classloader can see the JUnit jars loaded by Ivy, but the Split Classloader seems to be delegating incorrectly when trying to load classes used by the JUnit Runner.
换句话说:Ant 的 JUnit 任务有错误并且无法工作,我认为您受到了这个特定错误的影响.错误报告继续并列出了这些修复/解决方法:
In other words: Ant's JUnit task has bugs and won't work, and I think you are affected by this specific bug. The bug report goes on and lists these fixes / workarounds:
- 等待 Ant 1.9.1(错误报告被标记为已修复,预计很快就会发布)
- 将您的 JUnit JAR 复制到您的
ANTLIB
目录并继续使用 Ant 1.9.0.如果您想混合使用 JUnit 版本,这不是很好,但如果您只使用 4.10 左右,它应该可以工作. - 使用 Ant 1.8.4
- Wait for Ant 1.9.1 (the bug report is marked as fixed and the release can be expected very soon)
- Copy your JUnit JAR into your
ANTLIB
directory and keep using Ant 1.9.0. This is not so good if you want to mix JUnit versions, but it should work if all you use is 4.10 or so. - Use Ant 1.8.4
这篇关于运行junit任务时Bamboo Ant Task失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!