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?
You could try defining the Bar class as static:
... but the fact that it works in one environment but not in another suggests one of two things:
You can also annotate the nested class @Ignore if you don't want to exclude all inner classes.
This is because you included a nested class into junit fileset. Add an "excludes" property to your build.xml.
For example:
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 likemvn 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 onlycom.test.MyTestClass
but alsocom.test.MyTestClass.InnerClass
and evencom.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).