0

テストを実行しないと、コントローラーで Null Pointer Exception と言うエラーが発生します。すべて正常に動作します。

Controller :
@RequestMapping(value = "/studentinsection/{sectionId}", method = RequestMethod.GET)
    public ModelAndView studentInSectionForm(@ModelAttribute("studentInSectionFormData") StudentInSectionForm studentInSectionFormData,
                                             @PathVariable Integer sectionId,
                                             ModelMap model) {
        ArrayList<StudentInSections> studentInSectionList = (ArrayList<StudentInSections>)
                studentInSectionsService.retrieveAllStudentInSections(sectionId, 1);

        StudentSection studentSection = studentSectionService.retrieveStudentSection(sectionId);

        logger.info("section Name is:" + studentSection.getSectionName());

        ArrayList<User> userList = new ArrayList<User>();
        for (StudentInSections studentInSections : studentInSectionList) {
            String studentName =
                    (userService.retrieveUserName(studentInSections.getStudentId(), 1));
            User users = userService.retrieveUser(studentName);
            userList.add(users);
        }


        logger.info("sectionId is " + sectionId);

        ArrayList<User> allStudents = (ArrayList<User>)
                userService.retrieveAllStudents();

        studentInSectionFormData.setStudentInSectionList(studentInSectionList);
        model.addAttribute("studentList", allStudents);
        model.addAttribute("userList", userList);
        model.addAttribute("studentSectionName", studentSection.getSectionName());
        model.addAttribute("studentSectionId", studentSection.getSectionId());
        return new ModelAndView("studentinsection", "studentInSectionFormData", studentInSectionFormData);
    }


Testing is as follow:

    @Test
    public void testStudentInSectionForm() throws Exception {
        mockMvc.perform(get("/studentinsection/1"))
                .andExpect(status().isFound())
                .andExpect(redirectedUrl("studentinsection"));
    }

これは、sectionId がロガーで 1 と出力されても、studentin sectionList が nullMointerException を返す場合でも、すべてをコントローラーに正常に渡します。私の問題を解決するのを手伝ってください..ありがとう

4

1 に答える 1

0

コンテキストが正しく読み込まれていないようです。例外スタック トレースとは何ですか。

次の場合は、リクエストを表示することもできます。

  mockMvc.perform(get("/studentinsection/1"))
   .andExpect(status().isFound())
   .andDo(print())
于 2013-08-07T06:26:16.623 に答える