cWa*_*ren 6 java testing junit interface
假设定义了接口
interface Foo {
int getBaz();
void doBar();
}
Run Code Online (Sandbox Code Playgroud)
进一步假设合同规定每次doBar被称为baz都会增加.(好的,这是一个人为的代码,但在这里坚持我)
现在我想提供一个单元测试,我可以提供给Foo实施者,以便他们可以验证他们是否符合所有合同条件.
class FooTest {
protected Foo fooImpl;
@Test
public void int testBazIncrement()
{
int b = fooImpl getBaz();
fooImpl.doBar();
Assert.assertEquals( b+1, fooImpl.getBaz();
}
}
Run Code Online (Sandbox Code Playgroud)
将测试提供给Foo的实现者的最佳实践是什么?在我看来,需要一种机制让他们调用FooTest并提供Foo或FooFactory来构造Foo实例.此外,如果有很多测试(想想大接口),那么我想把所有这些测试放在一个FooTest类中.
有没有关于如何实施此类测试的最佳实践?