根据OP的描述,被模拟的主界面是Handler
接口:
public interface Handler {
void handle(String o);
}
所有三个模拟对象都应实现此接口,并且 TopHandler 类是被测类:
public static class TopHandler implements Handler {
private Handler a;
private Handler b;
public TopHandler(Handler a, Handler b) {
this.a = a; this.b = b;
}
public void handle(String o) {
a.handle(o);
b.handle(o);
}
}
测试用例必须验证TopHandler
实现确实调用了handle()
a 和 b 的方法:
@Test
public void testTopHandler() throws Exception {
Handler mockA = EasyMock.createMock(Handler.class);
mockA.handle("Test");
EasyMock.expectLastCall().once();
Handler mockB = EasyMock.createMock(Handler.class);
mockB.handle("Test");
EasyMock.expectLastCall().once();
TopHandler handler = new TopHandler(mockA, mockB);
EasyMock.replay(mockA, mockB);
handler.handle("Test");
EasyMock.verify(mockA, mockB);
}