单元测试代码如下:
@Test
public void cmsSynNewOrgAudTest() throws Exception {
CmsSynNewOrgAudReq request = new CmsSynNewOrgAudReq();
                //省略对request赋值部分
//......
String requestJson = JSONObject.toJSONString(request); MvcResult mvcResult = mockMvc
.perform(MockMvcRequestBuilders.post("/vl/cmsSynNewOrgAud").contentType(MediaType.APPLICATION_JSON_UTF8) // 发送数据的格式类型
.content(requestJson))
.andDo(print()).andExpect(status().isOk()).andReturn(); MockHttpServletResponse response = mvcResult.getResponse(); // 请求返回的结果
int httpStatus = response.getStatus();// 请求状态
logger.debug(httpStatus + ""); // 请求状态
String resContent = response.getContentAsString();// 返回结果用字符串显示
logger.debug(resContent); // 返回结果用字符串显示 JSONObject jsonObject = JSONObject.parseObject(resContent);
Assert.assertEquals(VlConst.VL_RETURNCODE_SUC, jsonObject.get("code"));
}控制器代码如下:
@RequestMapping(value = "/vl/cmsSynNewOrgAud", method = RequestMethod.POST)
public CmsSynNewOrgAudResp cmsSynNewOrgAud(@RequestBody @Valid CmsSynNewOrgAudReq request, Errors errors) {
CmsSynNewOrgAudResp resp = new CmsSynNewOrgAudResp();
if (errors.hasErrors()) {
resp.setCode(VlConst.VL_RETURNCODE_PARAMINVALID);// 参数非法
resp.setErrMsg(RequestBodyInvalidHandler.getFieldErrorMsg(errors));
resp.setSignature("");
resp.setTimestamp(System.currentTimeMillis());
} else {
resp = externalService4FXT.cmsSynNewOrgAud(request);
} return resp;
}使用mockmvc测试,竟然可以跳过@Valid参数校验进入service。而把requestJson 贴到postman里面发送post是可以检验出非法参数的。请问这个是什么啊?

解决方案 »

  1.   

    一下是你的代码,我帮你格式化一下在发布,否则抬不美观了@Test
    public void cmsSynNewOrgAudTest() throws Exception {
    CmsSynNewOrgAudReq request = new CmsSynNewOrgAudReq();
                    //省略对request赋值部分
    //......
    String requestJson = JSONObject.toJSONString(request);MvcResult mvcResult = mockMvc
    .perform(MockMvcRequestBuilders.post("/vl/cmsSynNewOrgAud").contentType(MediaType.APPLICATION_JSON_UTF8) // 发送数据的格式类型
    .content(requestJson))
    .andDo(print()).andExpect(status().isOk()).andReturn();MockHttpServletResponse response = mvcResult.getResponse(); // 请求返回的结果
    int httpStatus = response.getStatus();// 请求状态
    logger.debug(httpStatus + ""); // 请求状态
    String resContent = response.getContentAsString();// 返回结果用字符串显示
    logger.debug(resContent); // 返回结果用字符串显示JSONObject jsonObject = JSONObject.parseObject(resContent);
    Assert.assertEquals(VlConst.VL_RETURNCODE_SUC, jsonObject.get("code"));
    }控制器代码如下:
    @RequestMapping(value = "/vl/cmsSynNewOrgAud", method = RequestMethod.POST)
    public CmsSynNewOrgAudResp cmsSynNewOrgAud(@RequestBody @Valid CmsSynNewOrgAudReq request, Errors errors) {
    CmsSynNewOrgAudResp resp = new CmsSynNewOrgAudResp();
    if (errors.hasErrors()) {
    resp.setCode(VlConst.VL_RETURNCODE_PARAMINVALID);// 参数非法
    resp.setErrMsg(RequestBodyInvalidHandler.getFieldErrorMsg(errors));
    resp.setSignature("");
    resp.setTimestamp(System.currentTimeMillis());
    } else {
    resp = externalService4FXT.cmsSynNewOrgAud(request);
    }return resp;
    }
      

  2.   

    抱歉,格式化完忘了回答了。
    我认为你应该直接传一个对象而不是一个字符串(尽管它是json格式),因为你的参数注解是@RequestBody。
      

  3.   


    public MockHttpServletRequestBuilder content(String content) {
    try {
    this.content = content.getBytes("UTF-8");
    }
    catch (UnsupportedEncodingException e) {
    // should never happen
    }
    return this;
    }content方法要求传String
      

  4.   


    public MockHttpServletRequestBuilder content(String content) {
    try {
    this.content = content.getBytes("UTF-8");
    }
    catch (UnsupportedEncodingException e) {
    // should never happen
    }
    return this;
    }content方法要求传String不要笑得那么骚。
    我没用过这个东西,不过遇到过相似的问题,postman可以访问,而前端访问时后台拿不到参数。
    我写了一个HandlerInterceptor 的实现类,在preHandle打印了下请求的信息,发现点端倪
      

  5.   


    public MockHttpServletRequestBuilder content(String content) {
    try {
    this.content = content.getBytes("UTF-8");
    }
    catch (UnsupportedEncodingException e) {
    // should never happen
    }
    return this;
    }content方法要求传String不要笑得那么骚。
    我没用过这个东西,不过遇到过相似的问题,postman可以访问,而前端访问时后台拿不到参数。
    我写了一个HandlerInterceptor 的实现类,在preHandle打印了下请求的信息,发现点端倪我用firefox编辑发送http请求,也可以检验到非法参数,看样子是mockmvc模拟发送http请求有问题啊。大神你发现了什么端倪啊