aspose file tools*
The moose likes Testing and the fly likes JUnit and AbstractTestCase Problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » Testing
Bookmark "JUnit and AbstractTestCase Problem" Watch "JUnit and AbstractTestCase Problem" New topic
Author

JUnit and AbstractTestCase Problem

landon manning
Ranch Hand

Joined: Nov 20, 2000
Posts: 47
I'm trying to create an AbstractTestCase (see: http://www.placebosoft.com/abstract-test.html) and I am getting the following error (and, as far as I can tell, I shouldn't be getting this error):
[junit] Running AbstractMyTest
[junit] Tests run: 1, Failures: 1, Errors: 0, Time elapsed: 0.016 sec
[junit] Testsuite: AbstractMyClassTest
[junit] Tests run: 1, Failures: 1, Errors: 0, Time elapsed: 0.016 sec
[junit] Testcase: warning took 0 sec
[junit] FAILED
[junit] Cannot instantiate test case: testGetPolicies (java.lang.InstantiationException: AbstractMyClassTest
[junit] at java.lang.reflect.Constructor.newInstance(Native Method)
[junit] at junit.framework.TestSuite.addTestMethod(TestSuite.java:102)
[junit] at junit.framework.TestSuite.<init>(TestSuite.java:66)
[junit] at org.apache.tools.ant.taskdefs.optional.junit.JUnitTestRunner.<init>(JUnitTestRunner.java:261)
[junit] at org.apache.tools.ant.taskdefs.optional.junit.JUnitTestRunner.<init>(JUnitTestRunner.java:210)
[junit] at org.apache.tools.ant.taskdefs.optional.junit.JUnitTestRunner.main(JUnitTestRunner.java:520)
[junit] )
[junit] junit.framework.AssertionFailedError: Cannot instantiate test case: testMyMethod(java.lang.Instantiation
Exception: AbstractMyClassTest
[junit] at java.lang.reflect.Constructor.newInstance(Native Method)
[junit] )
[junit] Testcase: warning
BUILD FAILED
file :/src/build.xml:410: Test AbstractMyClassTest failed

My test class looks like this
<code>
...
import junit.framework.TestCase;
public abstract class AbstractMyClassTest extends TestCase
{

public abstract void setUp() throws IOException;


public AbstractMyClassTest(String arg0)
{
super(arg0);
}

public final void testMyMethod() throws IOException
{
//testing stuff
}

}
</code>

I'm using ant (version 1.5.1) with the following options:
<junit printsummary="yes" haltonfailure="yes" fork="yes" maxmemory="128M">
I'm using junit version 3.7 (as per the Version.class file in the junit.jar).

Anyone have any ideas/solutions?
[ June 19, 2003: Message edited by: landon manning ]
Richard Jensen
Ranch Hand

Joined: May 14, 2003
Posts: 67

<hr></blockquote>
What I see is that AbstractMyClassTest is abstract and then JUnit gets an error when it tries to instantiate an instance of the class via reflection. I wasn't able to follow the link in the URL (I got a page not found), so I don't know if you are copying an example or trying to figure out how to test an abstract class.
So, while the error you see makes sense to me I'm not sure what you're trying to accomplish.
[ June 19, 2003: Message edited by: Richard Jensen ]

Richard
N 37 33 W 122 18
landon manning
Ranch Hand

Joined: Nov 20, 2000
Posts: 47
(sorry about the confusion on what I'm trying to accomplish) What I'm trying to do is test an abstract class of mine, as well as all of the subclasses of my abstract class (without having to duplicate the tests defined in the abstract class's test class). The link above (I've corrected the link), explains abstract tests fairly well, as does http://c2.com/cgi/wiki?AbstractTestCases. JUnit even refers to this last link so I am under the impression that JUnit supports abstract test cases. ... however, upon looking at the source code for JUnit, I haven't found any code that suggests that JUnit actually does support abstract test cases.
So, at this point, I'm wondering if JUnit allows abstract test cases, and, if so, what am I doing wrong.
Chris Mathews
Ranch Hand

Joined: Jul 18, 2001
Posts: 2712
The point of an abstract TestCase (or any abstract class) is to have a number of concrete implementations. The benefit is that instead of duplicating logic in numerous classes you create a single abstract TestCase and extend it for each of your concrete ones. However, you still must have concrete implementations.
landon manning
Ranch Hand

Joined: Nov 20, 2000
Posts: 47
I do have concrete implementations. 2, actually, and they both work perfectly well, executing both their specific methods and the ones that they inherit from the AbstractMyClassTest. (Of course, this is when I execute them separately (I'm using Eclipse's junit plugin for this). )
It's when I try to run my ant build (that executes all of my test cases) that my AbstractMyClassTest.testMyMethod() fails.
One possible solution: Since the problem shows up when I do an ant build and execute the junt ant task, exclude the AbstractMyClassTest file from the fileset of tests in my build.xml. - Since none of the web sites have mentioned this step, I'm inclined to believe that I'm doing something else wrong, and would rather fix the problem than hack around it.
Richard Jensen
Ranch Hand

Joined: May 14, 2003
Posts: 67
Originally posted by landon manning:
It's when I try to run my ant build (that executes all of my test cases) that my AbstractMyClassTest.testMyMethod() fails.
...
One possible solution: Since the problem shows up when I do an ant build and execute the junt ant task, exclude the AbstractMyClassTest file from the fileset of tests in my build.xml.

I think this might be the solution. I did follow the link to the placebosoft.com site and read the article. Thanks for bringing it to my attention.
I think rather than "Abstract Test Cases" being 'supported' by JUnit what they are describing is a pattern of how to ensure the classes that derive from an abstract class or implement an interface conform to the expected behavior of the class/interface.
In the article on the placebosoft site he makes setup() final so that the concrete classes have to support a method that creates the 'TestableInterface'.
Now, it might be that if you chose 'Run as->Junit test' on your AbstractMyClassTest that you would get the same type of error. In that case, I do think that it is a matter of excluding the 'Abstract*Test' classes from being run when you do you ant task.
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Originally posted by landon manning:
One possible solution: Since the problem shows up when I do an ant build and execute the junt ant task, exclude the AbstractMyClassTest file from the fileset of tests in my build.xml. - Since none of the web sites have mentioned this step, I'm inclined to believe that I'm doing something else wrong, and would rather fix the problem than hack around it.

That's the correct solution. You don't want to run the abstract test case - it's only purpose is to function as template for the actual implementations. Therefore it shouldn't be included in your batchtest.


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Tim Harding
Greenhorn

Joined: Nov 21, 2004
Posts: 1
I've run into the same problem in Eclipse when trying to run all TestCase classes across my project.

I worked around the problem by creating a new parallel source directory named abstract-test that complements my src and test directories. These each contain a parallel package structure.

I've placed my abstract tests into the abstract-test directory, my concrete tests into the test directory and my subject code (interfaces and implementation classes) in the src directory.

I've created an Eclipse run configuration that runs all test cases in the test source tree. The abstract test cases are never executed... works like a charm.

Once I get to writing the Ant build.xml file I'll simply pick up the tests in the test directory once again.
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Originally posted by Tim Harding:
I've run into the same problem in Eclipse when trying to run all TestCase classes across my project.


I remember this bug, too, but I thought it was fixed now. What version are you using?
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: JUnit and AbstractTestCase Problem