MockitoDoSetup
Prefer using when/thenReturn over doReturn/when for additional type safety.

Severity
WARNING

The problem

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:

Suppression

Suppress false positives by adding the suppression annotation @SuppressWarnings("MockitoDoSetup") to the enclosing element.