我有测试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
和覆盖方法? Ex:
public class GreenFormTest extends FormTest {
Form form = new GreenForm();
@Override
public Form getForm() { return form; }
}
答案 0 :(得分:0)
您可以使用测试用例的setUp
()方法。
public class Form
{
public void doSomething(){}
}
public class GreenForm extends Form
{
@Override
public void doSomething(){}
}
通过测试:
public class FormTest extends TestCase
{
protected Form f;
@Before
public void setUp()
{
f = new Form();
}
@Test
public void testForm()
{
// do something with f
}
}
public class GreenFormTest extends FormTest
{
@Before
@Override
public void setUp()
{
f = new GreenForm();
}
}
答案 1 :(得分:0)
我同意你对如何测试这个并定期这样做的想法。我遵循的模式是:
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
}