Assuming you have such DateV1
and DateV2
:
public class DateV1 {
protected final Date date;
public DateV1(Date date) {
this.date = date;
}
public int getYear() {
return 1900 + date.getYear();
}
}
public class DateV2 extends DateV1 {
private Calendar cal;
public DateV2(Date date) {
super(date);
cal = Calendar.getInstance();
cal.setTime(date);
}
public int getYear() {
return cal.get(Calendar.YEAR);
}
}
The test might look like:
@RunWith(value = Parameterized.class)
public class DateTest {
private DateV1 date;
public DateTest(DateV1 date) {
this.date = date;
}
@Parameters
public static Collection<Object[]> data() {
final Date currDate = new Date();
Object[][] data = new Object[][] { { new DateV1(currDate) }, { new DateV2(currDate) } };
return Arrays.asList(data);
}
@Test
public void getYearShouldReturn2013() {
Assert.assertEquals(2013, date.getYear());
}
}
However, there is one important question: Should your super and sub classes (DateV1
and DateV2
) be tested in exactly the same way? Does the DateV2
class deliver exactly the same functionality as its superclass DateV1
? If there is absolutely no difference in behavior between these classes why have the two? If there is a difference between the two then it should be addressed by your test.