我测试的类收到一个客户端包装器:
测试类(snippest)
private ClientWrapper cw
public Tested(ClientWrapper cw) {
this.cw = cw;
}
public String get(Request request) {
return cw.getClient().get(request);
}
测试初始化:
ClientWrapper cw = Mockito.mock(ClientWrapper.class);
Client client = Mockito.mock(Client.class);
Mockito.when(cw.getClient()).thenReturn(client);
//Here is where I want to alternate the return value:
Mockito.when(client.get(Mockito.any(Request.class))).thenReturn("100");
在示例中,我总是返回“100”,但请求有一个属性 id
我想返回不同的值 client.get(Request)
基于 request.getId()
.
我该怎么做?
原文由 Roee Gavirel 发布,翻译遵循 CC BY-SA 4.0 许可协议
您可以使用 Mockito 的答案,而不是:
写: