我想在调用特定的模拟方法时执行一些代码。
mock = EasyMock.createMock(ConListener.class);
// Record n Replay
mock.Connected();
mock.DataSent();
EasyMock.replay(mock);
sock = createCon(addr, mock)
// I want the send to be called only after the mock's Connected() is invoked.
sock.send("data");
有什么方法可以实现在调用模拟时执行Invoke操作?
mock.Connected().Invoke () //something like this?
到目前为止,我有
mock = EasyMock.createMock(ConListener.class);
// Record n Replay
mock.Connected();
mock.DataSent();
EasyMock.replay(mock);
CompletableFuture<Void> connected = new CompletableFuture<Void>();
sock = createCon(addr, new ConListener() {
public void Connected() {
mock.Connected();
connected.complete(null);
}
public void DataSent() { mock.DataSent(); }
});
connected.get()
sock.send("data");
想检查是否有更好更好的方法。
答案 0 :(得分:0)
我找到了addDelegate
来解决我的问题。不是那么清晰直观,但是比我以前的方法要好一些。
interface ConListener {
void Connected();
void DataSent(int size);
}
// Having it as a class, so that I can only add stub codes,
// for a particular method and leave the rest.
class TestListener implements ConListener {
void Connected() {Assert.fail("")}
void DataSent(int size) {Assert.fail("")}
}
@Test
public void SomeTest() {
CompletableFuture<Void> connected = new CompletableFuture<Void>();
mock = EasyMock.createMock(ConListener.class);
// Record
mock.Connected();
EasyMock.expectLastCall().andDelegateTo(new TestListener() {
public void Connected() {
connected.complete(null);
}
});
EasyMock.expectLastCall().times(1); // This cannot come before adding delegates. Why?
EasyMock.replay(mock);
// Test
sock = createCon(addr, mock/*using actual mock*/);
connected.get(timeout, unit);
sock.send(blahblah);
}