我想为不同的接口实现运行相同的JUnit测试.我用@Parameter选项找到了一个很好的解决方案: public class InterfaceTest{ MyInterface interface; public InterfaceTest(MyInterface interface) { this.interface = interfa
public class InterfaceTest{
MyInterface interface;
public InterfaceTest(MyInterface interface) {
this.interface = interface;
}
@Parameters
public static Collection<Object[]> getParameters()
{
return Arrays.asList(new Object[][] {
{ new GoodInterfaceImpl() },
{ new AnotherInterfaceImpl() }
});
}
}
此测试将运行两次,首先使用GoodInterfaceImpl,然后使用AnotherInterfaceImpl类.但问题是我需要为大多数测试用例提供一个新对象.一个简化的例子:
@Test
public void isEmptyTest(){
assertTrue(interface.isEmpty());
}
@Test
public void insertTest(){
interface.insert(new Object());
assertFalse(interface.isEmpty());
}
如果在insertTest之后运行isEmptyTest则失败.
是否有选项可以使用新的实现实例自动运行每个测试用例?
BTW:为接口实现clear()或reset() – 方法实际上不是一个选项,因为我不需要它在生产代码中.
如果您在生产中不需要这样的东西,可能只在测试层次结构中创建工厂接口和实现,并使getParameters()返回工厂列表.然后,您可以在@Before带注释的方法中调用工厂,以便为每个测试方法运行获取正在测试的实际类的新实例.
