我的问题是关于测试一个实现了很多接口的类。例如,我有这个类:我们如何测试一个类实现了很多接口?
public class ServiceControllerImpl extends ServiceController implements IDataChanged, IEventChanged {
}
现在有两种测试方法。首先是对具体课程直接进行测试。这意味着对象类型是具体类而不是接口。
public class ServiceControllerImplTest {
ServiceControllerImpl instance;
@Before
public void setUp() {
instance = new ServiceControllerImpl();
// you can bring this instance anywhere
}
}
第二种方法是仅在接口上进行测试。我们必须将此对象转换为它实现的所有接口。
public class ServiceControllerImplTest {
ServiceController instance; // use interface here
IDataChanged dataChangeListener;
@Before
public void setUp() {
instance = new ServiceControllerImpl();
dataChangeListener = (IDataChanged) instance;
// instance and dataChangeListener "look like" two different object.
}
}
我倾向于第二种方案,因为也许在将来,我们可以改变它实现了与其他对象的接口,因此,使用具体类可能会导致在未来的测试失败。我不知道这个问题的最佳做法。
谢谢:)
对于测试您在接口类型上测试的接口。所以你的第二个解决方案基本上就是要走的路。 – JayC667
我确定上面的评论给出了答案,但你的问题需要澄清一点。当你写'dataChangeListener =(IDataChanged)dataChangeListener;'你的意思是'dataChangeListener =(IDataChanged)实例;',对吧? –
@DaveSchweisguth是的。我编辑过。非常感谢。 – hqt