用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类型的参数,多谢多谢!

解决方案 »

  1.   

    而且这个MockHttpServletRequestBuilder.param(String name, String... values),后一个参数似乎只支持传String?
    我就很疑惑了。
      

  2.   

    Long. toString()   Integer.toString();
      

  3.   


    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啊
      

  4.   

    @RequestParam(value="page") Integer page
    ·························································
    .param("page","1")
    都一样,参考上面
      

  5.   

    对呀,MockHttpServletRequestBuilder.param(String name, String... values),但是我们controller里面要求传入的是其他类型(Long,Integer...),有解决的吗
      

  6.   

    Long和Integer都是直接写,比如.param("id","1"),框架能自动转成(long)1。最傻的是list类型的param,直接写.param("list","1,2"),自动就转成["1","2"]的List<Long>类型了
      

  7.   

    MVC 微框架 http://www.verejava.com/?section_id=1697715673191