vendredi 16 avril 2021

NullPointerException error while running test

I am new at testing in Java / Spring and when running test, I encounter the following error:

java.lang.NullPointerException at com.company.core.service.impl.StudentServiceImpl.findAllStudentServiceImpl.java:52) at com.company.core.service.impl.integration.StudentServiceImplTest.given_Nothing_When_FindAll(StudentServiceImplTest.java:226)
...

When clicking to link, it points the following method:

public List<StudentDTO> findAll(StudentCriteriaRequest request, Sort sort) {
    if(request.isGraduated()) {
        return getStudentDTOS((studentRepository.findAll(request, sort)).getContent());
    }
    return getStudentDTOS(studentRepository.findAllAndOthers());
}

And here is the test method:

public void given_Nothing_When_FindAll() {
  List<StudentDTO> studentDTOList = new ArrayList<>();

  for (int i = 0; i < 3; i++) { // --> line 52
      CommandDTO commandDTO = createStudent("Student name" + i);
      StudentDTO studentDTO = studentService.findByUuid(commandDTO.getUuid());
      studentDTOList.add(studentDTO);
  }
  List<StudentDTO> response = studentService.findAll(new StudentCriteriaRequest(), Sort.unsorted()); // line 226 --> it throws error at this line while debugging test class
  
  assertEquals(4, response.size());
  assertTrue(response.stream().anyMatch(studentDTO -> studentDTO.getUuid().equals(brandDTO.getStudentUuid())));
  assertTrue(response.stream().anyMatch(studentDTO -> studentDTO.getUuid().equals(studentDTOList.get(0).getUuid())));
  assertTrue(response.stream().anyMatch(studentDTO -> studentDTO.getUuid().equals(studentDTOList.get(1).getUuid())));
  assertTrue(response.stream().anyMatch(studentDTO -> studentDTO.getUuid().equals(studentDTOList.get(2).getUuid())));
}

I have tried to check request parameter if it is null in the first class, but it does not make any sense. So, what is the cause of the problem? And should I fix it on service file (on former method) or test file (on latter block)? As I have not previous experience, I have really no idea about the solution of this problem. Any help would be appreciated.

Aucun commentaire:

Enregistrer un commentaire