What is the purpose of the @Mock and @InjectMocks annotations?

@Mock and @InjectMocks are Mockito annotations that reduce test boilerplate by automatically creating mock dependencies and wiring them into the class under test.

Key Points

Example

For a class OrderService(PaymentGateway gateway), declaring @Mock PaymentGateway gateway and @InjectMocks OrderService orderService lets Mockito construct orderService with the mocked gateway automatically, no manual wiring required.

Interview Tip

A concise interview answer is

@Mock creates a mock dependency without calling mock() manually, and @InjectMocks creates the class under test and automatically injects any matching @Mock fields into it, usually via its constructor. Together they cut down boilerplate setup code in every test class.

Last updated: 16 July 2026

Previous question: How do you mock an object in Mockito?

Next question: How do you use when and thenReturn in Mockito?

More Mockito interview questions

All Mockito questions