I am facing some issues with my stubbing with Mockito. In my @Test class, I am using userRes object for both stubbing, but I am getting NullPointerException in newService.checkBackground(newUser.getId(),false). I assume that the newUser.getId() is causing the issue because when i hardcode both the test call and change newUser.getId() to "444", it works.
Any ideas?
class MyService {
public void grantAccess() {
User user = new User();
user.setId("444");
User newUser = UserDao.putUserData(user);
newService.checkBackground(newUser.getId(), false);
}
}
This is the test I have written
@ExtendWith(MockitoExtention.class)
@MockitoSettings(strictness = Strictness.LENIENT)
class MyServiceTest {
@InjectMocks
private MyService myService;
@Test
void testMyStuff() {
User user = new User();
user.setId("444");
User userRes = new User();
userRes.setId("444");
try(MockedStatic<UserDao> userDao = mockStatic(UserDao.class)) {
userDao.when(() -> UserDao.putUserData(user)).thenReturn(userRes);
when(service.checkBackground(userRes.getId(), false)).thenReturn("test");
}
}
} //end class