Prefer using the format
when(mock.mockedMethod(...)).thenReturn(returnValue);
to initialise mocks, rather than,
doReturn(returnValue).when(mock).mockedMethod(...);
Mockito recommends the when/thenReturn syntax as it is both more readable
and provides type-safety: the return type of the stubbed method is checked
against the stubbed value at compile time.
There are certain situations where doReturn is required:
when makes
an actual method call.spy where the method call where calling the spied method
brings undesired side-effects.Suppress false positives by adding the suppression annotation @SuppressWarnings("MockitoDoSetup") to the enclosing element.