1

我所有的映射都正常工作:

 MvcResult mvcResult = this.mockMvc.perform(get("/company/doSomething"))
                    .andDo(print())
                    .andExpect(status().isOk())
                    .andReturn();

从此返回状态 ok

@RequestMapping(value = "/company/doSomething", method = RequestMethod.GET,produces = MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    public Boolean myMethod() {
      return false;
    }

如果我在控制器方法中设置断点并运行 test n 调试模式,则测试只是通过(未调用断点)并打印出响应 - 没有返回任何内容,我得到了这个:

MockHttpServletResponse:
              Status = 200
       Error message = null
             Headers = {}
        Content type = null
                Body = 
       Forwarded URL = default
      Redirected URL = null
             Cookies = []

实际的请求看起来像这样

MockHttpServletRequest:
         HTTP Method = GET
         Request URI = /company/doSomething
          Parameters = {}
             Headers = {}

             Handler:
                Type = org.springframework.web.servlet.resource.DefaultServletHttpRequestHandler
4

2 回答 2

2

你的类上有@Controller 注释吗?

您的类路径中还有 Jackson2 (JSON) 库吗?

当我复制您的代码并运行测试时,我得到正确的响应输出:

MockHttpServletResponse:
          Status = 200
   Error message = null
         Headers = {Content-Type=[application/json]}
    Content type = application/json
            Body = false
   Forwarded URL = null
  Redirected URL = null
         Cookies = []

但是我的请求输出说明了正确的处理程序

MockHttpServletRequest:
     HTTP Method = GET
     Request URI = /company/doSomething
      Parameters = {}
         Headers = {}
         Handler:
            Type = example.TestController
          Method = public java.lang.Boolean example.TestController.myMethod()

而您的输出表明默认的 DefaultServletHttpRequestHandler 处理程序正在处理请求。

于 2013-07-26T16:04:05.767 回答
2

从选择的实际处理程序(即 DefaultServletHttpRequestHandler)中,我可以看到没有匹配的带注释的控制器方法。不知何故,指定的请求与控制器方法请求映射不匹配。

于 2013-07-26T18:35:38.117 回答