java - Junit 扩展测试用例来测试 child

标签 java unit-testing junit tdd

我有类和测试用例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/

相关文章:

java - 大写问题,使用 IndexOf

java - 对于接受类似类型的输入参数、相同的实现代码但不同的输出的测试用例,正确的实现是什么

java - 使用 SpringSecurity 和 Mock 进行 Spring Boot 单元测试

java - 测试 Junit 和 Mockito 在分析日期和小时数时出现错误

java - Junit:测试方法中的 If 子句

java - 该继承程序在执行时给出输出 '2' 。如何?

java - Redis - 考虑重命名其中一个 bean 或通过设置 spring.main.allow-bean-definition-overriding=true 启用覆盖

java - 比较 Java 8 中的日期和 LocalDateTime

android - 如何将测试文件夹添加到较旧的 Android Studio 项目

java - 在 IntelliJ IDEA 2018.1 中运行 JUnit 5 测试时出错