0

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
purplewind
  • 331
  • 10
  • 26
  • 1
    Why are you mocking the service which you are testing? (`when(service.checkBackground…)`) – knittl Oct 11 '22 at 12:23
  • Does this answer your question? [What is a NullPointerException, and how do I fix it?](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – knittl Oct 11 '22 at 12:23

0 Answers0