2013-08-07 130 views
0

我在我的控制器中出現錯誤說出空指針異常,而當我不執行測試時。一切正常。對於SPRING-MVC的控制器測試

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。幫我解決我的問題.. Thanx

+0

我已一切正常。所有實體都有效。控制器中沒有值的簡單測試工作正常。 –

回答

0

它看起來像上下文未被正確加載。什麼是異常堆棧跟蹤。

您還可以查看請求,如果你這樣做:

mockMvc.perform(get("/studentinsection/1")) 
    .andExpect(status().isFound()) 
    .andDo(print()) 
+0

上下文正確加載。它適用於其他sontroller,我試過這個,但沒有改變記錄器的輸出。 –

+0

您有不同的測試環境嗎?顯示錯誤信息 – NimChimpsky

+0

FrameworkServlet'':在1ms內完成初始化 信息:co.softwarehouse.olt.web.controller.StudentInSectionController - 節Id是:1 測試運行:1,失敗:0,錯誤:1,跳過: 0,所用時間:0.223秒<<<失敗! testStudentInSectionForm(controller.StudentInSectionControllerTest)經過的時間:0.222秒<<<錯誤! org.springframework.web.util.NestedServletException:請求處理失敗;嵌套的異常是java.lang.NullPointerException –