mercredi 24 juin 2015

how to verify a method of a non-mock object is called?

It seems mockito only verify whether a method of a mock object is called and the mock object always have sth. like doReturn().when(mock object)...

But can I creat a mock object and define doReturn().when(mock object).. and then verify a method of another object is called?

Here is what I want to do: I define a mockEnvironment and return a response no matter what happen. But then I want to verify different methods of anotherObj is called in different cases.

How to do that?

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(...);
}

Aucun commentaire:

Enregistrer un commentaire