The Mockito documentation for doReturn()
states:
You can use doThrow(), doAnswer(), doNothing(), doReturn() and doCallRealMethod() in place of the corresponding call with when(), for any method. It is necessary when you
- stub void methods
- stub methods on spy objects (see below)
- stub the same method more than once, to change the behaviour of a mock in the middle of a test.
and also...
Use doReturn() in those rare occasions when you cannot use when(Object).
With an example given of...
when(mock.foo()).thenThrow(new RuntimeException());
//Impossible: the exception-stubbed foo() method is called so RuntimeException is thrown.
when(mock.foo()).thenReturn("bar");
//You have to use doReturn() for stubbing:
doReturn("bar").when(mock).foo();
In general, you should use the when(...).thenReturn(...)
syntax and the occasions when the doReturn(...).when(...)
syntax is useful are rare. However, it's important to note that the when(...)
pattern is required for mocking void methods, which are not rare. It's just the doReturn(...)
syntax which is less commonly used.
To specifically answer your questions:
No, the two syntaxes work slightly differently - the 'doReturn(...)' is able to set up the mock object to record a behaviour before the mocked method is called, whereas the 'when(...)' syntax works by doing some behind-the-scenes jiggery-pokery to set up a stub handler which the 'thenReturn(...)' method can work on. They'll usually have the same effect but the implementation differences become apparent in the corner case above.
For a mock, when(...)
calls the stubbed method on the mock object. That's why the corner case above around re-defining the stubs behaviour is important.