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

Abstract factory pattern

When single task can be done by multiple groups/family of objects and decision is taken at the runtime.