Re: Tests for several classes implementing a generic interface
kofa wrote:
Then, I'd like to have a test where I only need to change the line
that instantiates the object under test. I've come up with:
public class ThingTest<T> {
I haven't thought this through completely, but I think the correct way
to do this is to create this class with a constructor that takes a type
token. Example:
public class ThingTest<T> {
private Class<T> type;
public ThingTest( Class<T> type ) {
this.type = type;
}
....
}
Now you have some means of actually testing the type.
private Something<T> underTest = (Something<T>) new IntegerThing();
private Something<?> underTest = new IntegerThing();
@Test
public void test() {
T thingA = underTest.createThing();
T thingA = type.cast( underTest.createThing() );
...etc....
T thingB = underTest.createThing();
underTest.doSomething(thingA, thingB);
Set<T> result = underTest.getThings();
// assert whatever...
}
}
Furthermore, you might be able to type check the "underTest" object too.
public class ThingTest<T> {
private Class<T> type;
private Something<T> underTest;
public ThingTest( Class<T> type, Something<T> underTest ) {
this.type = type;
this.underTest = underTest;
}
....
}
Now you invoke your test object like this:
ThingTest<Integer> testHarness = new ThingTest<Integer>( Integer.class,
new IntegerThing() );
That combo of parameters is the only ones the compiler will allow.