What does the @InjectMocks annotation do in Mockito?
A) It injects mock dependencies into the object being tested
B) It verifies that the mocks are used correctly
C) It initializes the test class
D) It configures the database connection
Answer:
A) It injects mock dependencies into the object being tested
Explanation:
The @InjectMocks
annotation in Mockito is used to inject mock dependencies into the object being tested. When you use @InjectMocks
on a field, Mockito automatically injects the mocks (created with @Mock
or Mockito.mock()
) into the field, which represents the class under test.
This allows you to test the class in isolation, as the real dependencies are replaced with mock objects that simulate their behavior according to the test’s needs.