I've got an abstract generic collections class GCollection
, and a class that extends that called GStack
.
To test the implementation I have an abstract JUnit test class, which I extend for each of the GCollection
implementations I do:
public abstract class GCollectionTest<T extends GCollection<E>, E> {
private GCollection<? extends Object> collection;
protected abstract GCollection<T> createInstance();
@Before
public void setup() throws Exception {
collection = createInstance();
}
// Tests down here.
This is extended like so:
public class GStackCollectionInterfaceTest<S extends GCollection<E>> {
protected GDSStack<? extends Object> createInstance() {
return new GDSStack<String>();
}
}
I test first with a GStack
holding String
objects, then re-run the tests with Date
objects to ensure it works with different object types.
@Test
public void testIsEmpty() {
assertTrue(collection.isEmpty()); // Fresh Stack should hold no objects
collection.add(new String("Foo")); // Error here.
assertFalse(collection.isEmpty());
}
The error given is:
The method add(capture#24-of ? extends Object) in the type GCollection is not applicable for the arguments (String)
My understanding of the error is that I can't put a String
object into a GCollection<T extends GCollection<E>>
object, but I don't think that's what I'm trying to do.
What am I doing wrong?
How can I solve this error while maintaining tests that are as generic as possible?