首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >竹子蚂蚁任务运行junit任务失败

竹子蚂蚁任务运行junit任务失败
EN

Stack Overflow用户
提问于 2013-05-10 20:01:39
回答 1查看 4.2K关注 0票数 1

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

我的错误是什么?

SimplerTest.java

代码语言:javascript
复制
import static org.junit.Assert.*;

import org.junit.Test;

public class SimplerTest {


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

本地输出:

代码语言:javascript
复制
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

服务器输出:

代码语言:javascript
复制
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

代码语言:javascript
复制
<?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

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-05-11 04:16:23

感谢详细的Ant输出。

看起来你正在你的竹子服务器上运行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?”上引发的:

代码语言:javascript
复制
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,因为JUnit不在核心Ant类路径上,而是由常春藤添加到JUnit任务中)。由于某些JUnit类必须已经加载才能达到这一点,Ant Classloader可以看到JUnit加载的jars,但是当尝试加载JUnit运行器使用的类时,拆分类加载器似乎没有正确地委托。

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

  • 等待Ant1.9.1(错误报告被标记为已修复,很快就会发布)
  • 将您的JUnit JAR复制到您的ANTLIB目录中,并继续使用Ant1.9.0。如果您想混合使用JUnit版本,这就不太好了,但是如果您使用的都是4.10左右的版本,那么它应该可以工作。
票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/16481801

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档