2013-05-28 2 views
1

을 테스트하는 테스트 케이스를 확장 I가 클래스 테스트를 FormJunit와 자식

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 및 방법을 시험하는 방법이다 PROPPER 확장하고 테스트 케이스 FormTest? 예 :

public class GreenFormTest extends FormTest { 
    Form form = new GreenForm(); 

    @Override 
    public Form getForm() { return form; } 
} 
+1

당신은 상속 문법 몇 가지 문제를 갖고있는 것 같다. 'GreenForm'은 그것을 확장하지 않고 Form의 메소드를 오버라이드하고'GreenFormTest'는 그것의 부모 클래스의 이름을 잃어 버렸습니다. –

+0

@DanielLerps이 작은 문법 실수로 내 질문에 대해 혼동하지 않았 으면 좋겠다. –

답변

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 
} 
0

당신은 그것을 위해의 TestCase의 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(); 
    } 
} 
+0

이 방법은'FormTest'가'@ Before'에서 무엇이든 할 수 없게한다. –

+0

'super.setUp()'를 사용하여 FormTest가 원하는 모든 것을 실행합니다. 수퍼 호출 후에 자식 클래스의'setUp()'에 변경을 지정하면됩니다. –

+0

사실,'protected' 필드는 나쁜 습관입니다. 나는 이것을 매우 유사하지만 일반적으로 사용하는 형식을 사용하는 제안을했습니다. –