开发者

Error when defining inner classes in a Test class in JUnit

开发者 https://www.devze.com 2023-01-24 17:33 出处:网络
I\'m having some problems when defining inner classes in a Test class inherited from TestCase, for JUnit 3. Scenario is like following:

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?


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>  


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


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


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

0

精彩评论

暂无评论...
验证码 换一张
取 消