1

我有测试的类和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; }
}
4

2 回答 2

0

为此,您可以使用setUpTestCase 的 () 方法。

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();
    }
}
于 2013-05-28T14:24:57.137 回答
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
}
于 2013-05-28T14:43:22.803 回答