似乎嘲笑仅验证是否调用了嘲笑对象的方法,并且嘲笑对象始终具有类似 doReturn().when(mock object)
但是我可以创建一个模拟对象并定义doReturn().when(mock object)
然后验证另一个对象的方法是否被调用吗?
这是我想做的事情:我定义了一个模拟环境,无论发生什么情况都返回一个响应。但是然后我想验证在不同情况下调用anotherObj的不同方法。
怎么做?
public class BaseClass {
private Environment mockEnvironment;
@Test
public void testcase () {
setMockitoEnvironment();
response = foo(mockEnvironment, argument1);
verify(anotherObj).codePath1(...);
response = foo(mockEnvironment, argument2);
verify(anotherObj).codePath2(...);
}
}
//this method successfully return a response with any input
//because I do not care how response is eventually generated,
//I only care whether code path reaches createResponse() via
//code path 1 or code path 2.
private void setMockitoEnvironment() {
mockEnvironment = mock(Environment.class);
doReturn (response).when(mockEnvironment).createResponse(for any input);
}
private Response foo(...) {
...
return createResponse(...);
}