I have the following code snippet (Java 17, junit 5), and I am trying to get coverage on a test case where the inSellerList is null.
public class MyClass() {
public myMethod {
ResDTO resDto = doSomething();
}
private ResDTO doSomething() {
ResDTO resDto = new ResDTO();
// ...
List<SellerDTO> editSellerListRes = editSellerList(resDto.getSellerList()); // <---- I need to mock this so that resDto.getSellerList() is null
resDto.setSellerList(editSellerListRes);
return resDto;
}
private List<SellerDTO> editSellerListRes editSellerList(List<SellerDTO> inSellerList) {
if (inSellerList == null || inSellerList.size() == 0) {
System.out.println("I need to have this part covered in the test")
List<SellerDTO> = new ArrayList<>();
}
}
}
In my test scenario, I am using @MockBean to mock resDto.getSellerList so that it returns null, but based on the coverage report, the code inside the if block is not getting covered.
I am limited to only using junit 5 and mockito.
@SpringBootTest(classes = {MyClass.class})
class MyClassTest {
@Autowired MyClass myClass;
@MockBean
private ResDTO resDto;
@Test
private void test1() {
// does not work
when(resDto.getSellerList()).thenReturn(null)
// does not work either
doAnswer(invocation -> {
resDto.setSellerList(null);
return null;
}).when(resDto).setSellerList(anyList());
myClass.doSomething()
// assertions
}
}