What is the difference between Mockito.mock(SomeClass) and the @Mock annotation?

They both achieve the same result. Using an annotation (@Mock) is usually considered "cleaner", as you don't fill up your code with boilerplate assignments that all look the same.

Note that in order to use the @Mock annotation, your test class should be annotated with @RunWith(MockitoJUnitRunner.class) or contain a call to MockitoAnnotations.initMocks(this) in its @Before method.


The difference is in the lines of code you need to write :) :) :)

Seriously though, using the annotations has the exact same effect as using the Mockito.mock.

To quote the documentation of MockitoAnnotations the use of annotations has the following benefits:

  • Allows shorthand creation of objects required for testing.

  • Minimizes repetitive mock creation code.

  • Makes the test class more readable.

  • Makes the verification error easier to read because field name is
    used to identify the mock.

The javadoc for MockitoAnnotations is here


There are two significant advantages to using the annotation.

  • A mock created with @Mock can be injected into the class you're testing, using the @InjectMocks annotation. This is a powerful technique that can make testing significantly easier. It just won't work with mocks created by the mock method.
  • If you have any errors involving your mock, the name of the mock will appear in the message. If you've used @Mock, then this name will just be the name of the field. This makes it really easy to find the problem mock.

Of course, in addition to these two important advantages, most people find the @Mock notation much more readable, and it does cut down on the amount of code. I see no reason not to use it.