我写了一个接口MyInterface
,它将由不同的实现者实现。通用JUnit测试类
我也写了一个类MyInterfaceTest
,其中包含所有实现者应该能够用来测试他们的实现的通用测试方法。
我只是不知道如何使它作为JUnit测试工作。
目前,我有这样的事情:
public class MyInterfaceTest {
private static MyInterface theImplementationToTest = null;
@BeforeClass public static void setUpBeforeClass() throws Exception {
// put your implementation here:
theImplementationToTest = new Implementation(...);
}
@AfterClass public static void tearDownAfterClass() throws Exception {
theImplementationToTest = null;
}
@Test public void test1() { /* uses theImplementationToTest */ }
@Test public void test2() { /* uses theImplementationToTest */ }
}
我用静态方法setUpBeforeClass
因为每个执行的初始化需要花费很多时间,所以我想为所有测试一次初始化。
有了这个版本的测试,实现者必须更改setUpBeforeClass
的代码,并把它们自己的实现。
我确信还有另外一种方法来编写MyInterfaceTest
,这样实现者只需要继承它或者发送一个参数,而不需要修改代码。但是,我在JUnit中没有足够的经验来使其工作。你能告诉我如何做到这一点?
这是如何帮助解决原来的问题? – 2012-07-22 14:58:05
原始问题是什么? – 2012-07-24 16:01:34
如何重用静态设置/拆卸实现,并根据子类的不同而不同。正如我的回答中所述。 – 2012-07-27 03:04:53