154

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

But can I create 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 happens. 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(...);
}
Community
  • 1
  • 1
user389955
  • 9,605
  • 14
  • 56
  • 98

2 Answers2

180

You can use a Mockito Spy for this. If you setup anotherObj as a spy you can verify method calls on that object. In your example you need to make sure that the call to foo uses the spy instead of an ordinary implementation of anotherObj. The spy is setup something like this:

AnotherClass anotherObjSpy = Mockito.spy(new AnotherClass());
// do stuff -- e.g. anotherObjSpy.foo(...);
verify(anotherObjSpy).codePath1(...);
geco17
  • 5,152
  • 3
  • 21
  • 38
K Erlandsson
  • 13,408
  • 6
  • 51
  • 67
  • 2
    That means I may need to create and inject multiple mocked objects if I want to verify multiple things? that is not convenient. – user389955 Jun 26 '15 at 06:21
  • @user389955 yes, you need to setup each object you want to check as a spy. As far as I know there is no way around that. Mockito needs to add a proxy to the objects and someone must tell Mockito which objects to proxy. – K Erlandsson Jun 26 '15 at 06:31
  • 11
    Instead of saying "//do stuff" it might be helpful to add that it is the "anotherObjSpy" that must make the method call being tested. – jwehrle May 04 '17 at 22:48
  • Thanks @jwehrle, you need to use the anotherObjSpy in the calls, otherwise the tests won't work. – argoth Dec 11 '18 at 09:40
  • I have a final method which I can't call spy on – Akshay Hazari Mar 29 '21 at 09:08
11

Annotate the non-mock object with @Spy annotation and then check for verify(). Check this

CodeHunter
  • 2,017
  • 2
  • 21
  • 47
  • Nice. works fine if we dont have to initialise the `@Spy` annotated object. But if we want to initialise it in `@Before` then dont use `@Spy` annotation but do it inside the `@Test` class as "val someName = Mockito.spy(objectYouWantToSpy)" – firstpostcommenter Aug 14 '23 at 11:19