用SpringMVC做的项目,在对Controller做Junit单元测试时,听说要用MockMVC,不明白在测试Post请求时如何传非String类型的参数,求大神指点!Controller 定义如下:
@RequestMapping(value = { "/insert.action" })
@ResponseBody
public String insert(HttpServletRequest request,
@RequestParam(value = "item_id", required = true) Long item_id,
@RequestParam(value = "qi_id", required = true) Long qi_id,
@RequestParam(value = "type", required = true) Integer type,
@RequestParam(value = "value", required = true) String value) throws Exception {
this.subjectService.addEntertainment(item_id, qi_id, type, value);
return this.createSuccessResponse(null, null, request);
}这里要传的参数有Long和Integer类型的。在网上搜的很多资料都是传的String参数的,如http://blog.csdn.net/xiejx618/article/details/21201007
但是假如我如下写(uri是传进去的参数,偷懒只写了两个param)
mockMvc.perform(MockMvcRequestBuilders.post(uri)
.param("item_id", "123L").param("qi_id", "123L")).andExpect(status().isOk());
会报错说:Failed to convert value of type 'java.lang.String' to required type 'java.lang.Long'; nested exception is java.lang.NumberFormatException: For input string: "123L"求指点这种情况应该怎么写mockMVC传递Long、Integer类型的参数,多谢多谢!
@RequestMapping(value = { "/insert.action" })
@ResponseBody
public String insert(HttpServletRequest request,
@RequestParam(value = "item_id", required = true) Long item_id,
@RequestParam(value = "qi_id", required = true) Long qi_id,
@RequestParam(value = "type", required = true) Integer type,
@RequestParam(value = "value", required = true) String value) throws Exception {
this.subjectService.addEntertainment(item_id, qi_id, type, value);
return this.createSuccessResponse(null, null, request);
}这里要传的参数有Long和Integer类型的。在网上搜的很多资料都是传的String参数的,如http://blog.csdn.net/xiejx618/article/details/21201007
但是假如我如下写(uri是传进去的参数,偷懒只写了两个param)
mockMvc.perform(MockMvcRequestBuilders.post(uri)
.param("item_id", "123L").param("qi_id", "123L")).andExpect(status().isOk());
会报错说:Failed to convert value of type 'java.lang.String' to required type 'java.lang.Long'; nested exception is java.lang.NumberFormatException: For input string: "123L"求指点这种情况应该怎么写mockMVC传递Long、Integer类型的参数,多谢多谢!
我就很疑惑了。
public String insert(HttpServletRequest request,
@RequestParam(value = "item_id", required = true) Long item_id,
@RequestParam(value = "qi_id", required = true) Long qi_id,
@RequestParam(value = "type", required = true) Integer type,
@RequestParam(value = "value", required = true) String value) 问题是接收的参数是Long啊
·························································
.param("page","1")
都一样,参考上面