Monday, October 11, 2021

@Mock,@InjectMocks and @RunWith(MockitoJunitRunner.class) in mockito

 In previous example we have created a Mock like below-

DataService dataServiceMock = mock(DataService.class);

but we can create mock using @Mock annotations.

@Mock

DataService dataServiceMock;

when mock is created then we need to inject into beans 

@InjectMocks

SomeBusinessImpl businessImpl;// dataServiceMock will inject into SomeBusinessImpl

Then we no need to inject this dependencies through constructor or setter injection.

MockitoJUnitRunner checks if all settings are ok or any mistakes have take places.

so put it to the top of the test class

@RunWith(MockitoJUnitRunner.class)


No comments:

Post a Comment

Fluent interface pattern

 public class UserConfigurationManager {     private String userName;     private String password;     private UserConfigurationManager() { ...