我有类和测试用例FormTest
,用于测试Form
public class Form {
public doSomething() {}
}
public class GreenForm extends Form {
@Override
public doSomething() {}
}
public class YellowForm extends Form {
}
public class FormTest {
Form form = new Form();
@Test
public void doSomethingTest() { getForm().doSomething() }
public Form getForm() { return form; }
}
扩展 FormTest
是测试 GreenForm
和重写方法的正确方法吗? 例如:
public class GreenFormTest extends FormTest {
Form form = new GreenForm();
@Override
public Form getForm() { return form; }
}
最佳答案
我同意您关于如何测试并定期执行此操作的想法。我遵循的模式是这样的:
public class FormTest{
private Form form;
@Before
public void setup(){
// any other needed setup
form = getForm();
// any other needed setup
}
protected Form getForm(){
return new Form();
}
@Test
// do tests of Form class
}
public class GreenTest{
private GreenForm form;
@Before
public void setup(){
form = getForm();
// any other needed setup
super.setup();
// any other needed setup
}
@Override
protected Form getForm(){
return new GreenForm();
}
@Test
// do tests of how GreenForm is different from Form
// you might also need to override specific tests if behavior of the method
// under test is different
}
关于java - Junit 扩展测试用例来测试 child ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16793903/