【问题标题】:Bamboo Ant Task fails when running junit task运行junit任务时Bamboo Ant Task失败
【发布时间】:2013-05-05 02:39:53
【问题描述】:

在我当前的项目中,我正在使用 junit 测试。在我的本地电脑上运行我的 ant 文件会按预期生成我的测试报告,但是当竹子尝试运行我的测试时,它会生成以下输出。

我的错误是什么?

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

构建.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

【问题讨论】:

  • 这就是 Bamboo 服务器的所有日志吗? NoClassDefFoundError 主要是由于找不到其他类或无法运行其静态代码而引起的,因此该错误的真正堆栈跟踪或日志中更高的先前异常将有很大帮助。你能以某种方式运行带有详细日志记录的 ant 构建吗?
  • 我在问题的末尾添加了 ant -v 的输出。

标签: java junit ant bamboo


【解决方案1】:

感谢 Ant 的详细输出。

您的 Bamboo 服务器上似乎正在运行 Ant 1.9.0。 Ant 错误跟踪器中存在一个已知问题(错误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,但是 拆分类加载器在尝试时似乎委派不正确 加载 JUnit Runner 使用的类。

换句话说:Ant 的 JUnit 任务有 bug 并且无法工作,我认为你受到了这个特定 bug 的影响。错误报告继续并列出了这些修复/解决方法:

  • 等待 Ant 1.9.1(错误报告已标记为已修复,预计很快会发布)
  • 将您的 JUnit JAR 复制到您的 ANTLIB 目录并继续使用 Ant 1.9.0。如果您想混合 JUnit 版本,这不是很好,但如果您使用的只是 4.10 左右,它应该可以工作。
  • 使用 Ant 1.8.4

【讨论】:

  • 不错。谢谢。我切换回 Ant 1.8.4。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-10-05
  • 1970-01-01
  • 2011-12-05
  • 2011-09-15
  • 2014-03-09
  • 2023-03-17
  • 2012-07-11
相关资源
最近更新 更多