Methods should not be directly invoked on mocks. Should this be part of a verify(..) call?


The problem

Direct invocations on mocks should be avoided in tests.

When you call a method on a mock, the call normally does only what you have configured it to do (through calls to when(...).thenReturn/thenAnswer, etc.) and makes a record of the call that can be read by later verify(...) calls. Both of these are rarely what you want:

Sometimes, test authors, especially those familiar with other mocking frameworks (like EasyMock), will call a method on a mock for one of two reasons:

  1. By default, EasyMock requires the test setup to call every method that the code under test will call. Mockito’s defaults do not require this.
  2. Many EasyMock tests choose to have EasyMock require that the code under test call all the methods that the test setup calls. Mockito tests that want to verify calls to methods must verify each call individually. To check that any particular method has been called, a Mockito test must call verify(foo).bar(), and it must do so after the code under test has run.
public void balanceIsChecked() {
  Account account = mock(Account.class);
  LoanChecker loanChecker = new LoanChecker(account);


  // Should be verify(account).checkBalance();, or be removed if the call to
  // `checkEligibility` is sufficient proof the code is behaving as intended.

There is at least one edge case in which a call to a mock has different effects: A call to a final method will normally not be intercepted by Mockito, so it will run the implementation of that method in the code under test. Sometimes, that method will call other methods on the mock object, producing effects similar to if the test had called those methods directly. Sometimes, the method will have other effects. Both kinds of effects can be confusing, so prefer to avoid such calls when possible.


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