我有两个实现相同接口的具体类。两者的测试都是相同的,所以我想做1参数化类测试。 类最初只需要配置一次(使用相同的设置),所以这是我的解决方法:
@RunWith(Parameterized.class)
public class MyInterfaceTest {
private MyInterface i;
private static boolean setupDone[] = new boolean[2]; //this is ugly
public MyInterfaceTest(MyInterface i) {
this.i = i;
if(i instanceof ImplA && !setupDone[0]){
setup();
setupDone[0] = true;
}else if(i instanceof ImplB && !setupDone[1]){
setup();
setupDone[1] = true;
}
//must add code here for more Impl -> ugly
}
@Parameterized.Parameters
public static Collection<Object[]> getParameters()
{
return Arrays.asList(new Object[][] {
{ new ImplA() },
{ new ImplB() }
});
}
//tets...
}
哪个很难看。什么是更好的解决方案?
答案 0 :(得分:0)
@RunWith(Parameterized.class)
public class ServiceLocator1Test {
private MyInterface i;
public MyInterfaceTest(MyInterface i) {
this.i = i;
}
@Parameterized.Parameters
public static Collection<Object[]> getParameters()
{
MyInterface a = new ImplA(); setup(a);
MyInterface b = new ImplB(); setup(b);
return Arrays.asList(new Object[][] {
{ a },
{ b }
});
}
//tets...
}
private static void setup(MyInterface myinterface){/*...*/}