1
我在寫的JUnit用如下的方法簽名與控制器的方法之一的製作工序的檢驗方法論證:設定的HttpServletRequest的使用MvcMock
@RequestMapping(value="/getTokenizedURL.json",method=RequestMethod.POST)
@ResponseBody
public ResponseData getTokenizedURL(@RequestBody final RequestData requestData, final HttpServletRequest request) throws CustomException
我需要使用MockMvc調用這個方法,我可以使用以下調用:
mockMvc.perform(post("/user/getTokenizedURL.json")
.contentType(MediaType.APPLICATION_JSON)
.content(json))
.andDo(print())
.andExpect(status().isOk());
但問題是我無法設置HttpServletRequest
參數調用時使用模擬MVC中的原始方法。沒有設置HttpServletRequest
的說法,我的測試給出的問題,因爲它是必需的東西,並在原始方法中使用。
請讓我知道我應該如何設置相同。謝謝!
Ohh yes ..明白了。我想添加屬性在會議裏面請求,我可以使用下面: MockHttpSession session = new MockHttpSession(); session.setAttribute(「username」,「[email protected]」); (「/ user/getTokenizedURL.json」)。contentType(MediaType.APPLICATION_JSON).content(json).session(session).servletPath(「/ user/getTokenizedURL.json」); ()())。和Expect(status()。isOk()); 許多感謝Sotirios! –