@Mock tells Mockito to create a mock object for a field, so you can replace a real collaborator with a fake whose behaviour you control and whose calls you can verify. It is the annotation equivalent of calling Mockito.mock(Type.class), and it keeps test classes shorter when you need several mocks.
The annotation does nothing on its own. A Mockito extension, runner or MockitoAnnotations.openMocks call has to initialise the fields before each test.
A mock is a generated subclass or proxy of a type. By default every method returns a neutral value (null, 0, false, an empty collection or an empty Optional) and records each call. You then tell the mock what to return with when(...).thenReturn(...) and check interactions with verify(...).
These two declarations are equivalent:
The annotation form is preferred when the same mock is used by several tests in a class.
Use MockitoExtension from the mockito-junit-jupiter artifact:
Here @Mock creates repo, and @InjectMocks builds UserService, passing the mock to its constructor (or setter, or field if needed). MockitoExtension also enables strict stubs: an unused when(...) stubbing fails the test with UnnecessaryStubbingException, which keeps tests honest.
| Approach | When to use |
|---|---|
@ExtendWith(MockitoExtension.class) | JUnit 5, the default choice |
MockitoAnnotations.openMocks(this) | Manual setup, returns an AutoCloseable you should close in @AfterEach |
@RunWith(MockitoJUnitRunner.class) | JUnit 4 only |
MockitoJUnit.rule() | JUnit 4 rules |
MockitoAnnotations.initMocks(this) is deprecated and was replaced by openMocks. Mockito 5 also uses the inline mock maker by default, which means final classes and methods can be mocked without extra configuration.
@Mock accepts attributes for common needs:
answer changes the default return behaviour (RETURNS_DEEP_STUBS, CALLS_REAL_METHODS, RETURNS_SMART_NULLS).name appears in failure messages.lenient = true disables strict-stub failures for that mock.extraInterfaces makes the mock implement additional interfaces.| Annotation | Purpose |
|---|---|
@Mock | Full fake. Nothing real runs unless you stub it to. |
@Spy | Wraps a real object. Real methods run unless you stub them. |
@InjectMocks | Creates the class under test and injects mocks and spies into it. |
@Captor | Creates an ArgumentCaptor for inspecting arguments. |
Prefer a mock for collaborators with I/O, such as repositories and HTTP clients. Use a spy sparingly, for example when you need to keep most of a legacy class's behaviour and override one method.
openMocks, which leaves the field null and causes a NullPointerException.when(spy.method()), which calls the real method. Use doReturn(x).when(spy).method().@Mock where a Spring test needs a bean in the context. That is the job of @MockitoBean in Spring Framework 6.2 and later.Yes, with the same default behaviour. The annotation just declares the mock as a field and needs an extension or openMocks to initialise it.
Mockito was never asked to process the test class. Add @ExtendWith(MockitoExtension.class) (JUnit 5) or call MockitoAnnotations.openMocks(this).
Yes in Mockito 5, which uses the inline mock maker by default. Older versions required enabling mockito-inline.
@Mock is plain Mockito and knows nothing about Spring. @MockBean (deprecated, see its replacement @MockitoBean) puts the mock into a Spring ApplicationContext.
@Mock declares a Mockito mock as a field and removes boilerplate when tests share collaborators. Initialise it with MockitoExtension, pair it with @InjectMocks, and stub or verify with when and verify. Use it for plain unit tests, and use Spring's @MockitoBean when the mock must live in a Spring context.