Wednesday, July 24, 2019

Mock Vs Injectmocks In Mockito

@InjectMockscreates objects and inject mocked dependencies
This is useful while we need to create class instances (mock object) which is depend on other class like services.
Example We need to test UserController class methods and this controller methods are depends on UserService. So in this case we will use @InjectMocks on UserController class and @Mock on UserService class.
Use @InjectMocks when we need all internal dependencies initialized with mock objects to work method correctly.

@Mockcreates mocks
This is useful when we need to create mocks object which are needed to support testing of class to be tested.
We must define the when-thenRetrun methods for mock objects with class methods which will be invoke during test case execution. Like we need to use @Mock on UserService class.

No comments:

Post a Comment