@InjectMocks – creates 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.
@Mock – creates 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.
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.
@Mock – creates 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