What is the purpose of the @Mock annotation in Mockito?
A) To create and inject mock objects
B) To configure database access
C) To mark a class as a test suite
D) To generate logs during testing
Answer:
A) To create and inject mock objects
Explanation:
The @Mock
annotation in Mockito is used to create and inject mock objects into your test cases. When you annotate a field with @Mock
, Mockito automatically creates a mock instance of that field’s type, allowing you to simulate its behavior during the test.
This annotation is often used in combination with @InjectMocks
to inject the mocked dependencies into the class under test, making it easier to write unit tests that are isolated from the actual dependencies.