• @Mock creates a mock
  • @Spy creates a spy
  • @InjectMocks creates an instance of the class and injects the mocks that are created with the @Mock or @Spy annotations into this instance

In JUnit4 to initialize these mocks and inject them, you either use: @RunWith(MockitoJUnitRunner.class) or Mockito.initMocks(this).

In JUnit5 to initialize these mocks and inject them, you must use @ExtendWith(MockitoExtension.class).

@RunWith(MockitoJUnitRunner.class)  // JUnit 4 option 1
@ExtendWith(MockitoExtension.class) // JUnit 5
public class SomeManagerTest {

    @InjectMocks
    private SomeManager someManager;
    @Mock
    private SomeDependency someDependency; // this mock will be injected into someManager

	@Test
	void test() {
		Mockito.initMocks(this);    // JUnit4 option 2
		// continue test
	}
}

public class SomeManager {
	public SomeManager(SomeDependency someDependency) { }
}

public class SomeDependency { }