代码之家  ›  专栏  ›  技术社区  ›  Filippo

JUnit-java。lang.NullPointerException:无法调用“…”因为“this.modelMapper”为空

  •  0
  • Filippo  · 技术社区  · 3 年前

    我学Java已经四个月了,所以请原谅我的基本错误。

    我正在尝试从我的服务层对一个方法进行单元测试:

        @Override
        @Transactional
        public List<StudentDto> getStudentList() {
            List<Student> tempList = studentDAO.getStudentList();
    
            List<StudentDto> tempListStudentDto = new ArrayList<>();
    
            for (Student theStudent: tempList) {
                tempListStudentDto.add(convertToDto(theStudent));
            }
    
            return tempListStudentDto;
        }
    

    通过这个@Test:

        // JUnit test for getStudentList()
        @Test
        @DisplayName("getStudentList()")
        public void givenStudentList_whenGettingList_thenReturnList() {
            // given -precondition
            BDDMockito.given(studentDao.getStudentList())
                    .willReturn(List.of(newStudentOne, newStudentTwo));
    
            // when - behaviour that we are going to test
    
            List<StudentDto> studentList = studentService.getStudentList();
    
            // then - verify the output
            assertAll(
                    () -> org.assertj.core.api.Assertions.assertThat(studentList).isNotNull(),
                    () -> org.assertj.core.api.Assertions.assertThat(studentList).size().isEqualTo(2)
            );
    

    我经常会犯这样的错误: JAVAlang.NullPointerException:无法调用“org.modelmapper.modelmapper.getConfiguration()”,因为“this.modelmapper”为空

    你能在这里帮助我吗,或者告诉我如何以可测试的方式将学生类转换为DTO?

    我在谷歌上搜索了这个问题,尝试了所有的建议,但都没有成功。

    0 回复  |  直到 3 年前