Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Java by (3.9k points)
What is the difference between @Mock and @InjectMocks in Mockito framework?

1 Answer

0 votes
by (46k points)

@Mock makes a mock. @InjectMocks generates an example of the class and injects the mocks that are designed with the @Mock (or @Spy) annotations within this case.

See that you must practice @RunWith(MockitoJUnitRunner.class) or Mockito.initMocks(this) to initialize certain mocks and inject them.

@RunWith(MockitoJUnitRunner.class)

public class SomeManagerTest {

    @InjectMocks

    private SomeManager someManager;

    @Mock

    private SomeDependency someDependency; // this will be injected into someManager

     //tests...

}

Related questions

0 votes
1 answer
0 votes
1 answer
0 votes
1 answer

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...