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

setUpそのために TestCaseの () メソッドを使用できます。

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 に答える