J
J
JavaDeveloper982019-12-01 20:41:29
Java
JavaDeveloper98, 2019-12-01 20:41:29

NullPointException in Unit tests what to do?

@ExtendWith(MockitoExtension.class)
@RunWith(JUnitPlatform.class)
class UserControllerTest {
@InjectMocks
UserController userController;
@Mock
UserDao userDao;
@Test
public void testGetAllUsers() throws Exception {
// given
User user1= new User(UUID.fromString("2bf303c7-9999-4e3c-a14d-457251cb4d8d"), "Vasya Petrov", "+79050379948");
User user2= new User(UUID.fromString("2bf303c7-1111-4e3c-a14d-457251cb4d8d"), "Vera Sidorova", "+79110372248");
List users = new ArrayList<>();
users.add(user1);
users.add(user2);
when(userDao.
// when
List result = userController.getAllUsers();
// then
assertThat( result.size()).isEqualTo(2);
}
Crashes on NullPointException don't know how to solve it

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Gornostaev, 2019-12-01
@sergey-gornostaev

In the stacktrace, you can see the entire chain of calls leading to the NPE, and understand for which call you forgot to describe the behavior of the mock.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question