I'd like to do something like this in jUnit:
@Runwith(Parameterized.class)
public abstract class BaseTest {
protected abstract List<Object[]> extraParams();
protected abstract ClassUnderTest testObject;
@Parameters
public Collection<Object[]> data() {
List<Object> params = ...; // a standard set of tests
params.addAll(extraParams());
return params;
}
@Test
public doTest() {
// assert things about testObject
}
}
public class ConcreteTest extends BaseTest {
protected ClassUnderTest = new ConcreteClass(...);
protected List<Object[]) extraParams() {
List<Object> extraParams = ...; // tests specific to this concrete type
return extraParams;
}
}
So that by extending this class, I run a bunch of standard tests against the object under test, plus some extra ones specified in the concrete class.
However, jUnit requires that the @Parameters
method is static. How else can I tidily achieve the aim, of having a set of standard parameters plus extra ones in the concrete classes?
The best I've come up with so far is to have an un-annotated Collection<Object[]> standardParams()
in the abstract class, and to require that the subclass contain a method:
@Parameters
public Collection<Object[]> data() {
List<Object> params = standardParams();
params.addAll(...); // extra params
return params;
}
... but this isn't as tidy as I'd like, as it puts too much responsibility on the writer of the subclass.