1

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?

Anushka
  • 31
  • 4
  • This is going to depend on more details of `MongoOperationsUtil` than you've given us. – Louis Wasserman Jan 25 '22 at 18:49
  • Mongo operations util is for crud operations and checking existence of data in db, basic operations – Anushka Jan 26 '22 at 02:38
  • Don’t use field injection; it largely defeats the point of using DI. Whilst Mockito can be made to inject random private fields, it’s better in every way to use constructor injection; or setter injection at a stretch. – Boris the Spider Jan 26 '22 at 06:12

1 Answers1

0
@Autowired
private MongoOperationsUtil myUtil;

you use @Autowired in myUtil. So the object is injected into spring container.

If you want to mock it in your test case, try to use @MockBean for MongoOperationUtil field.

Then you could try

    Mockito.when(myUtil.checkExistance(XXX)).thenReturn(YYY);

MinandLucy
  • 76
  • 4
  • I have added test class logic, can u tell what the issue could be, its is not ensuring coverage, all lines are not getting covered? – Anushka Jan 26 '22 at 06:44
  • "all lines are not getting covered", do you mean all lines in MongoOperationsUtil not coverted? Because you used Mock, it didn't really run the logic in your MongoOperationsUtil class. Unit test should just test your Service class, if you want improve coverage, you may just write anther ut in MongoOperationsUtil. – MinandLucy Jan 26 '22 at 06:47
  • all lines of the service layer method are not getting covered, I am not checking coverage of util claass here, just the service layermethod. – Anushka Jan 26 '22 at 06:55
  • Looks like you use MockitoJUnitRunner which is specific for use with the Mockito test framework. You may use SpringRunner and use MockBean in MongoOperationsUtil and Autowired for your service class. And the below thread may help you . https://stackoverflow.com/questions/56738078/whats-the-difference-between-runwithmockitojunitrunner-class-and-runwithsp/56745558 – MinandLucy Jan 26 '22 at 07:20
  • I used @RunWith(SpringRunner.class) as well, but does not help. dont know why its failing to show coverage? – Anushka Jan 26 '22 at 07:31