在JUnit中的Test类中定义内部类时出错 [英] Error when defining inner classes in a Test class in JUnit

查看:458
本文介绍了在JUnit中的Test类中定义内部类时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在从TestCase继承的Test类中为JUnit 3定义内部类时遇到一些问题。场景如下:

I'm having some problems when defining inner classes in a Test class inherited from TestCase, for JUnit 3. Scenario is like following:

Foo.java

public class Foo {
  public void method() { ... }
}

FooTest.java

FooTest.java

public class FooTest extends TestCase {
  public class Bar extends Foo {
    public void method() { ... }
  }
  public void testMethod() { ... }
}

现在,如果我从Eclipse运行它,测试运行正常,但是如果我尝试从失败的Ant任务运行:

Now, if I run this from Eclipse, the tests run ok, but if I try to run from an Ant task it fails:

[junit] junit.framework.AssertionFailedError:Class Foo $ Bar没有公共构造函数TestCase(String name)或TestCase ()

[junit] junit.framework.AssertionFailedError: Class Foo$Bar has no public constructor TestCase(String name) or TestCase()

Bar不是Test类,它只是Foo的一个子类,它覆盖了一些方法,在测试时我不需要做真正的事情。

Bar is NOT a Test class, it's just a subclass of Foo overriding some method that I don't need to do the real stuff when testing.

我现在很迷茫,我不知道怎么回事o解决这个问题。将子类创建为独立的唯一方法是什么?

I'm quite lost at the moment and I don't know how to approach this problem. Is the only way to create the subclasses as standalone?

推荐答案

这是因为您在junit文件集中包含了一个嵌套类。
在build.xml中添加excludes属性。

This is because you included a nested class into junit fileset. Add an "excludes" property to your build.xml.

例如:

<target name="test" depends="test-compile">
    <junit>
        <batchtest todir="${test.build.dir}" unless="testcase">
            <fileset dir="${test.build.classes}"
                includes = "**/Test*.class"
                excludes = "**/*$*.class"/>
        </batchtest>
    </junit>
</target>  

这篇关于在JUnit中的Test类中定义内部类时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆