Search code examples
javareflectionjunitinner-classesjunit3

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:

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?


Solution

  • 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>