单元测试体系结构问题
2022-09-03 13:16:33
						因此,我开始为以下代码位布局单元测试:
public interface MyInterface {
  void MyInterfaceMethod1();
  void MyInterfaceMethod2();
}
public class MyImplementation1 implements MyInterface {
  void MyInterfaceMethod1() {
    // do something
  }
  void MyInterfaceMethod2() {
    // do something else
  }
  void SubRoutineP() {
    // other functionality specific to this implementation
  }
}
public class MyImplementation2 implements MyInterface {
  void MyInterfaceMethod1() {
    // do a 3rd thing
  }
  void MyInterfaceMethod2() {
    // do something completely different
  }
  void SubRoutineQ() {
    // other functionality specific to this implementation
  }
}
有几个实现,并期望更多。
我最初的想法是节省自己用这样的东西重写单元测试的时间:
public abstract class MyInterfaceTester {
  protected MyInterface m_object;  
  @Setup
  public void setUp() {
    m_object = getTestedImplementation();
  }
  public abstract MyInterface getTestedImplementation();
  @Test
  public void testMyInterfaceMethod1() {
    // use m_object to run tests
  }
  @Test
  public void testMyInterfaceMethod2() {
    // use m_object to run tests
  }
}
然后,我可以很容易地对其进行子类化以测试特定于实现的附加方法,如下所示:
public class MyImplementation1Tester extends MyInterfaceTester {
  public MyInterface getTestedImplementation() {
    return new MyImplementation1();
  }
  @Test
  public void testSubRoutineP() {
    // use m_object to run tests
  }
}
同样适用于 Implmentation 2 以后。
所以我的问题是:有什么理由不这样做吗?JUnit似乎很喜欢它,它满足了我的需求,但是我没有在我读过的任何单元测试书籍和示例中看到类似的东西。
我是否无意中违反了一些最佳实践?我是否在路上让自己心痛?有没有一种我没有考虑过的更好的方法?
感谢您的任何帮助。