Error defining inner classes in Test class in JUnit

I'm having some problems defining inner classes in the Test class inherited from TestCase for JUnit 3. The script looks like this:

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 fine, but if I try to execute the Ant task, this will not work:

[junit] junit.framework.AssertionFailedError: the Foo $ Bar class does not have a public constructor called TestCase (string name) or TestCase ()

Bar is not a Test class, it is just a subclass of Foo that overrides some method that I do not need to do with real things when testing.

At the moment, Iโ€™m completely lost, and I donโ€™t know how to approach this problem. The only way to subclass as standalone?

+9
java reflection junit inner-classes junit3
Nov 11 '10 at 0:40
source share
4 answers

This is because you included the nested file set in the junit file set. Add the excludes property to the build.xml file.

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> 
+15
Mar 22 '12 at 9:52
source share

You can try to define 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:

  • Java version
  • Classpath
  • [Edit: as shown by Jim below] Various versions of junit.jar
+5
Nov 11 '10 at 3:23
source share

I feel like a necrop, but the fact is that I ran into a similar problem with maven today.

Regular mvn test works well, but when I want to run tests from a specific package, for example, mvn test -Dtest=com.test.* - initializationError . This "works" for Junit 3 and 4.

I found the reason for my maven-case, this may be the same for ant. The fact is that by default the maven test plugin (surefire is) treats only a certain subset of all classes as โ€œtest classesโ€, namely, searching for them by name, for example * Test, etc. (You can read about it in the correct homepage ). When we define the test property, we completely override the default behavior. This means that with -Dtest=com.test.* Surefire will select not only com.test.MyTestClass , but also com.test.MyTestClass.InnerClass and even com.test.MyTestClass$1 (i.e. anonymous classes).

Thus, in order to execute, for example, classes from some package, you should use something like -Dtest=com.test.*Test (if you, of course, use suffixes to identify test classes).

+4
Mar 25 2018-11-11T00:
source share

You can also annotate the @Ignore nested class if you do not want to exclude all inner classes.

+3
Jul 29 2018-12-12T00:
source share



All Articles