在 JUnit 的测试类中定义内部类时出错

发布于 2024-10-02 07:50:37 字数 674 浏览 5 评论 0原文

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

Foo.java

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

FooTest.java

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

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

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

Bar is NOT a Test class, it's just a subclass of Foo overrideing一些我在测试时不需要做真正的事情的方法。

我现在很迷茫,不知道如何解决这个问题。是独立创建子类的唯一方法吗?

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

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

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 has no public constructor TestCase(String name) or TestCase()

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.

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

稍尽春風 2024-10-09 07:50:37

这是因为您将嵌套类包含到 junit 文件集中。
将“excludes”属性添加到您的 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>  

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

For example:

<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>  
情深缘浅 2024-10-09 07:50:37

您可以尝试将 Bar 类定义为静态:

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

...但它在一种环境中工作但在另一种环境中不起作用这一事实表明以下两件事之一:

  1. Java 版本类
  2. 路径
  3. [编辑:如下面吉姆的建议] junit.jar 的不同版本

You could try defining the Bar class as static:

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

... but the fact that it works in one environment but not in another suggests one of two things:

  1. Java version
  2. Classpath
  3. [Edit: as suggested by Jim below] Different versions of junit.jar
失眠症患者 2024-10-09 07:50:37

我感觉自己像个死人,但问题是我今天在 Maven 上遇到了类似的问题。

通常的 mvn test 运行良好,但是当我想从特定包运行测试时,例如 mvn test -Dtest=com.test.* - 抛出 initializationError 。这对 Junit 3 和 4 都“有效”。

我找到了我的 Maven 案例的原因,这对于 ant 可能是相同的。
事情是:默认情况下,maven 的测试插件(surefire 即)仅将所有类的特定子集视为“测试类”,即按名称搜索它们,例如 *Test 等(您可以在 surefire的主页)。当我们定义test 属性我们完全覆盖默认行为。这意味着使用 -Dtest=com.test.* Surefire 不仅会获取 com.test.MyTestClass,还会获取 com.test.MyTestClass.InnerClass< /code> 甚至 com.test.MyTestClass$1 (即匿名类)。

因此,为了运行某个包中的类,您应该使用类似 -Dtest=com.test.*Test 的内容(当然,如果您使用后缀来标识测试类)。

I'm feeling like a necrposter, but the thing is that I've ran into similar problem with maven today.

Usual mvn test runs well but when I want run tests from specific package like mvn test -Dtest=com.test.* - initializationError is thrown. This "works" for both Junit 3 and 4.

I found the reason for my maven-case, this may be the same for ant.
The thing is: by default maven's test plugin (surefire that is) considers only specific subset of all classes as "test-classes", namely searching them by name, like *Test and so on (you can read about this at surefire's home page).When we define test property we completely override default behavior. This means that with -Dtest=com.test.* surefire will pick up not only com.test.MyTestClass but also com.test.MyTestClass.InnerClass and even com.test.MyTestClass$1 (i.e. anonymous classes).

So in order to run e.g. classes from some package you should use something like -Dtest=com.test.*Test (if you use suffixes for identifying test-classes of course).

我不是你的备胎 2024-10-09 07:50:37

如果您不想排除所有内部类,也可以在嵌套类上注释@Ignore。

You can also annotate the nested class @Ignore if you don't want to exclude all inner classes.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文