How to mock helper methods used in service class. I tried to search but couldn't find, can you guide me?How to mock helper methods used in service class. I tried to search but couldn't find, can you guide me?
Service class function:
@Autowired
private MongoOperationsUtil myUtil;
@Override
public MyResponse getUsage(MyData input)
{
myUtil.checkExistance(input.getName);
MyResponse resp = new MyResponse();
resp.setUsage(usage);
resp.setMetaInfo(input);
return resp;
}
Helper class method - content:
@Autowired
private RedisUtil redisUtil;
///RedisUtil is a class I created for crud ops in redis
public void checkExistance(String name){
boolean inputFound = redisUtil.isKeyExists(name);
if (!inputFound) {
redisUtil.insert(name);
}
else{
redisUtil.update(name);
}
}
this is test logic: @SpringBootTest @RunWith(MockitoJUnitRunner.class) public class ApiUsageServiceTests { MongoOperationsUtil mongoUtilMock =Mockito.mock(MongoOperationsUtil.class);
@Test
public void getUsageTest() throws Exception{
System.out.println("Checking getUsage() from service layer");
doNothing().when(mongoUtilMock).checkExistance(anyString());
// when()
mongoUtilMock.checkExistance(Mockito.anyString());
verify(mongoUtilMock,
times(1)).checkExistance(Mockito.anyString());
}}
Now I need to write testcase for getUsage, by mocking checkExistance helper method, so how to do that to ensure coverage of LOC?