How do you mock objects in Mockito when using constructor injection?

Constructor injection makes mocking straightforward in Mockito: you create mocks for each dependency, then pass them directly into the constructor of the class under test.

Key Points

Example

For a class OrderService(PaymentGateway gateway, InventoryClient inventory), a test simply builds mock(PaymentGateway.class) and mock(InventoryClient.class) and constructs OrderService with them before stubbing behavior.

@ExtendWith(MockitoExtension.class)
class OrderServiceTest {

    @Mock
    private PaymentGateway gateway;

    @Mock
    private InventoryClient inventory;

    @InjectMocks
    private OrderService orderService;

    @Test
    void placesOrderWhenPaymentSucceeds() {
        when(gateway.charge(anyDouble())).thenReturn(true);
        assertTrue(orderService.placeOrder("SKU-1", 2));
    }
}

Interview Tip

A concise interview answer is

With constructor injection I mock each dependency with mock(), then construct the class under test with those mocks directly, or let @Mock plus @InjectMocks wire them automatically. It's the cleanest form of dependency injection to test because there's no field reflection involved.

Last updated: 16 July 2026

Previous question: What is the difference between a stub and a mock?

Next question: Can you explain Mockito's RETURNS_DEEP_STUBS and its use case?

More Mockito interview questions

All Mockito questions