I am writing a simple test case but getting a null pointer exception in the return value. Although when I debug the actual mocking is working, and my main code is also being triggered
My Test Case
@Test
public void testEmptyRequests() {
List<MyPojo> list = new ArrayList();
String env = "env-stub";
when(myService.fetchRecords(env)).thenReturn(list);
Assert.assertEquals(Integer.valueOf(0), myController.process(env, 1));
}
My Method
@Async
public Integer process(String environment, Integer id) {
List<MyPojo> list = myService.fetchRecords(environment);
if (list == null || list .isEmpty()) {
log.info("[0] claims to process");
return 0;
}
// Other logic here, Not relevant to this test case
}
change your test method to this:
@Test
public void testEmptyRequests() {
List<MyPojo> list = new ArrayList();
String env = "env-stub";
when(myService.fetchRecords(Mockito.anyString())).thenReturn(list);
Assert.assertEquals(Integer.valueOf(0), myController.process(env, 1));
}
You should mock the method you are using inside your code.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.