Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 203
Gothmog Avatar asked Nov 11 '10 00:11

Gothmog


People also ask

How do you write Junit for inner class methods?

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.

In which case do we need to define 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.

How do you declare an inner class?

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.

Can we define class inside method?

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.


2 Answers

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>  
like image 91
Kai Zhang Avatar answered Nov 02 '22 19:11

Kai Zhang


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
like image 30
Jason Avatar answered Nov 02 '22 19:11

Jason