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?
First, we have to add a new inner class called A to our test class and annotate the inner class with the @Nested annotation. After we have created the A class, we have to add one setup, teardown, and test method to the created inner class.
We use inner classes to logically group classes and interfaces in one place to be more readable and maintainable. Additionally, it can access all the members of the outer class, including private data members and methods.
To instantiate an inner class, you must first instantiate the outer class. Then, create the inner object within the outer object with this syntax: OuterClass outerObject = new OuterClass(); OuterClass. InnerClass innerObject = outerObject.
In Java, we can write a class within a method and this will be a local type. Like local variables, the scope of the inner class is restricted within the method. A method-local inner class can be instantiated only within the method where the inner class is defined.
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:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With