1
我試圖測試用這種方法的控制裝置的彈簧控制器的方法:測試具有@ModelAttribute作爲參數
@RequestMapping(值=「/測試」)
公共的ModelAndView generateRecords(@ModelAttribute(「僱員」 )員工僱員){
我想知道如何創建一個單元測試來測試這個。目前我使用:
MockHttpServletRequest request = new MockHttpServletRequest();
request.setRequestURI("/test");
//request.setMethod("GET");
new AnnotationMethodHandlerAdapter().handle(request,
new MockHttpServletResponse(), this.controller);
運行中NULL值的ModelAttribute(員工)該測試結果
有沒有辦法做集成測試時傳遞的ModelAttribute對象控制器?
感謝
只是概括地說:
解決這個問題挑HTML元素的名稱和在MockHttpRequest對象填寫放慢參數值,並且將它傳遞。
實施例:
MockHttpServletRequest httpServletRequest = MockRequestResponseGenerator.mockRequest(getServletInstance().getServletContext(), "POST", "/test", paramters);
//These paramters must be part of the ModelAttribute Object. Make sure, you are using custom property binding in case you have different object.
httpServletRequest.setParameter("name", "SPRING MVC INTEGRATION TEST
TEMP");
httpServletRequest.setParameter("id", "1");
httpServletRequest.setParameter("desc", "SPRING MVC INTEGRATION TEST DESC");
getServletInstance().service(httpServletRequest, httpServletResponse);
謝謝Teja,它工作!乾杯:) – Raj
好聽...接受答案,如果它有助於解決您的問題,以便其他用戶知道... –
如果員工有例如類型公司的成員「公司」 ,比方說,名稱和說明字段)?你如何表達通往company.name和company.description的路徑? – Rolf