我测试的类接收一个客户端封装器:
被测试的类(片段)
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
,我想根据request.getId()
的值返回不同的值给client.get(Request)
。 我该怎么做呢?我始终返回"100",但是请求有一个属性
id
,我想根据request.getId()
的值返回不同的值给client.get(Request)
。如何实现?
Request
是您正在尝试测试/模拟的类。 - John Montgomery