竹蚁任务运行junit任务失败



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

我错在哪里?

SimplerTest.java

import static org.junit.Assert.*;
import org.junit.Test;
public class SimplerTest {

@Test
public void dummerTest()
{
    assertTrue(true);
}
}
本地输出:

Buildfile: C:Usersjussigitkingdom-builder-repositorybuild.xml
compile-test:
[javac] Compiling 1 source file to C:Usersjussigitkingdom-builder-repositorybin
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输出。

看起来您正在您的Bamboo服务器上运行Ant 1.9.0。有一个已知的问题(bug 54835 - Classpath使用似乎在junit ant任务中被打破?)在ant bug跟踪器中,这是由SO上类似问题的海报开始的:"在ant, Ivy和junit中找不到类- 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类加载器处理system Classloader不了解JUnit,因为JUnit不在核心Ant Classpath,但由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

最新更新