0

我有一个如下所示的客户端类:

class MyClient{
    Service service;

    public MyClient(Service service){ ... }

    public boolean CoreFunction(Object arg1, Object arg2){
        Service.AnotherClass instance = new Service.AnotherClass(arg1, arg2);
        service.call(instance);
        if(instance.isSuccessful()) { ...; return true; }
        else { ...; return false; }
    }
}

实例的 isSuccessful() 返回一个标志,该标志将由 service.call() 内部更新。如果 instance.isSuccessful() 是真假,我想测试逻辑。如何使用 EasyMock 来实现这一点?谢谢你。

4

1 回答 1

0

您可以使用IAnswer拦截service呼叫并设置successful您的属性instance

Service service = EasyMock.createMock(Service.class);

// andAnswer style
service.call(anyObject(Service.AnotherClass.class));
expectLastCall().andAnswer(new IAnswer<Object>() {
    public Object answer() throws Throwable {
        Service.AnotherClass arg = (Service.AnotherClass) EasyMock.getCurrentArguments()[0];
        arg.setSuccessful(true); // or false in another test
        return null;
    }
});
于 2012-12-19T22:48:20.273 回答