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 に答える