似乎mockito只验证是否调用了模拟对象的方法,并且模拟对象始终具有某些功能。比如doReturn()。when(模拟对象)......
但我可以创建一个模拟对象并定义doReturn()。when(模拟对象).. 然后验证另一个对象的方法被调用?
这就是我想要做的事情:我定义了一个mockEnvironment并返回一个响应,无论发生什么。但后来我想验证在不同情况下调用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(...);
}