I am trying to write contract tests for some widely used interfaces:
Along the lines of:
public abstract class MyInterfaceContractTest extends TestCase {
private MyInterface _toTest;
public void setUp(){
_toTest = getTestableImplementation();
}
protected abstract MyInterface getTestableImplementation();
public void testContract(){
}
}
...and...
public class MyInterfaceImplementationTest extends MyInterfaceContractTest {
protected MyInterface getTestableImplementation(){
return new MyInterfaceImplementation(...);
}
}
However, I want to be able to test multiple instances of MyInterfaceImplementation
. In my use case, this is an immutable object containing a collection of data (with accessors specified as per the interface MyInterface
), and it might be empty, or have a small amount of data, or even lots of data.
So the question is, how can I test multiple instances of my implementations?
At the moment, I have to initialise the implementation to pass it into the abstract contract test. One approach would be to have multiple test classes for each implementation, where each test class tests a particular instance of that implementation - but that then seems a bit voluminous and difficult to keep track of.
FWIW, I'm using JUnit 3.