Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JUnit and AbstractTestCase Problem

 
landon manning
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

<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 ]
 
landon manning
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
(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
Posts: 2712
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Tim Harding
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic