Error when defining inner classes in a Test class

2019-04-06 06:44发布

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?

4条回答
迷人小祖宗
2楼-- · 2019-04-06 07:30

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
查看更多
在下西门庆
3楼-- · 2019-04-06 07:32

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

查看更多
甜甜的少女心
4楼-- · 2019-04-06 07:40

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>  
查看更多
forever°为你锁心
5楼-- · 2019-04-06 07:47

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).

查看更多
登录 后发表回答