我可以(我想更重要的是,我应该)在抽象类中包装通用 junit 测试吗?
例如,我有一组正在测试的类,它们都使用缓存 - 显然在那里进行测试以确保我实际上正确地缓存了东西会很好......测试代码几乎相同每个类,但有一些关于创建缓存对象的小细节。
做这样的事情可以吗?
abstract class Foo {
Baz baz;
@Before
abstract void setUp();
@Test
public void testMyBaz() {
// ...
}
}
public class Bar extends Foo {
void setUp() {
// set my baz
}
// other, more specific tests
}
最佳答案
这样做是可以的,但在大多数情况下,当你需要它时,你只需要数据工厂,在 JUnit 中是这样完成的:
@RunWith(Parametrized.class)
public class SumTest {
@Parameters
public static Iterable<Object[]> data() {
return Arrays.asList(new Object[][] {{1, 2, 3}, {3, 4, 7}});
}
private int a, b, sum;
public SumTest(int a, int b, int sum) {
this.a = a;
this.b = b;
this.sum = sum;
}
@Test
public void testSum() {
Assert.assertEquals(a + b, sum);
}
}
关于java - 使用抽象类进行通用测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33174201/