简体   繁体   English

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

[英]Error when defining inner classes in a Test class in JUnit

I'm having some problems when defining inner classes in a Test class inherited from TestCase, for JUnit 3. Scenario is like following: 我在从TestCase继承的Test类中为JUnit 3定义内部类时遇到一些问题。场景如下:

Foo.java 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() { ... }
}

Now, if I run this from Eclipse, the tests run ok, but if I try to run from an Ant task it fails: 现在,如果我从Eclipse运行它,测试运行正常,但如果我尝试从Ant任务运行它失败:

[junit] junit.framework.AssertionFailedError: Class Foo$Bar has no public constructor TestCase(String name) or TestCase() [junit] junit.framework.AssertionFailedError:类Foo $ Bar没有公共构造函数TestCase(String name)或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. Bar不是Test类,它只是Foo的一个子类,它覆盖了一些方法,在测试时我不需要做真正的事情。

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? 是独立创建子类的唯一方法吗?

This is because you included a nested class into junit fileset. 这是因为您在junit文件集中包含了一个嵌套类。 Add an "excludes" property to your build.xml. 在build.xml中添加“excludes”属性。

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>  

You could try defining the Bar class as static: 您可以尝试将Bar类定义为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 Java版本
  2. Classpath 类路径
  3. [Edit: as suggested by Jim below] Different versions of junit.jar [编辑:如下面Jim所建议的]不同版本的junit.jar

I'm feeling like a necrposter, but the thing is that I've ran into similar problem with maven today. 我觉得自己像个傻瓜,但问题是我今天遇到了与maven类似的问题。

Usual mvn test runs well but when I want run tests from specific package like mvn test -Dtest=com.test.* - initializationError is thrown. 通常的mvn test运行良好,但是当我想从特定的包运行测试时,例如mvn test -Dtest=com.test.* - 抛出了initializationError This "works" for both Junit 3 and 4. 这对于Junit 3和4都“有效”。

I found the reason for my maven-case, this may be the same for ant. 我找到了我的maven-case的原因,这对于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. 问题是:默认情况下,maven的测试插件(确定是的)只将所有类的特定子集视为“测试类”,即按名称搜索它们,如* Test等(您可以在surefire的主页上阅读此内容)当我们定义test属性时,我们完全覆盖默认行为。 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 (ie anonymous classes). 这意味着使用-Dtest=com.test.*不仅会获取com.test.MyTestClass ,还会获得com.test.MyTestClass.InnerClass甚至com.test.MyTestClass$1 (即匿名类)。

So in order to run eg classes from some package you should use something like -Dtest=com.test.*Test (if you use suffixes for identifying test-classes of course). 因此,为了运行某些包中的类,您应该使用类似-Dtest=com.test.*Test (如果您使用后缀来识别测试类)。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM