完了406NOT_ACCEPTABLE-SpringBootでのWebLayerのテスト

Dec 07 2020

Spring Bootv2.1.0.RELEASEアプリにこのメソッドがあります。

@GetMapping(value = "/wildProject", produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<List<WildProject>> getList(HttpServletRequest request,
        HttpServletResponse response)
        throws Exception {

    List<WildProject> list = authorisationService.getList();

    System.out.println("-----------------");
    System.out.println(list);
    System.out.println("-----------------");

    return ok().body(list);


} 

そしてこのテスト:

 this.mockMvc.perform(get("/wildProject")
  //.accept(MediaType.APPLICATION_JSON_UTF8_VALUE))
  // .andDo(print())
  .andExpect(content().contentType(MediaType.APPLICATION_JSON_VALUE))
  .andExpect(status().isOk());

これはテストの結果です:

20:03:38.253 [main] DEBUG o.s.w.s.m.m.a.HttpEntityMethodProcessor - Using 'application/json', given [*/*] and supported [application/json]
20:03:38.255 [main] WARN  o.s.w.s.m.s.DefaultHandlerExceptionResolver - Resolved [org.springframework.web.HttpMediaTypeNotAcceptableException: Could not find acceptable representation]
20:03:38.256 [main] DEBUG o.s.t.w.s.TestDispatcherServlet - Completed 406 NOT_ACCEPTABLE

MockHttpServletRequest:
      HTTP Method = GET
      Request URI = /wildProject
       Parameters = {}
          Headers = {Content-Type=[application/json;charset=UTF-8]}
             Body = null
    Session Attrs = {}

Handler:
             Type = com.bonansa.controller.AuthorisationController
           Method = public org.springframework.http.ResponseEntity<java.util.List<com.bonansa.WildProject>> com.bonansa.controller.AuthorisationController.getList() throws java.lang.Exception

Async:
    Async started = false
     Async result = null

Resolved Exception:
             Type = org.springframework.web.HttpMediaTypeNotAcceptableException

ModelAndView:
        View name = null
             View = null
            Model = null

FlashMap:
       Attributes = null

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

そして

@JsonSerialize(as = IWildProject.class)
public interface IWildProject {
..
}

回答

s7vr Dec 19 2020 at 04:59

あなたの問題は2つあります。

json応答を記述できるように適切なメッセージコンバーターを登録していない根本的な問題。これは、コントローラーが生成アノテーションを指定し、クライアントがすべてのヘッダーを受け入れるため、サーバーが設定ミスに対して406ではなく500を返す必要があるというバグのために非表示になっています。

詳細- https://github.com/spring-projects/spring-framework/issues/23287

これは、2.1.x SpringBootバージョンの最新リリースのみの問題ではなくなりました。

2 x80486 Dec 08 2020 at 00:48

何も送信せず、何かContent-TypeGET要求しているので、要求のヘッダーを設定する必要はありません。Acceptヘッダは、この場合には探しているものです。

また、MediaType.APPLICATION_JSON_VALUE一致しませんMediaType.APPLICATION_JSON_UTF8_VALUE

そのコードを次のようにリファクタリングすることをお勧めします。

this.mockMvc.perform(get("/wildProject")
  .accept(MediaType.APPLICATION_JSON_VALUE))
  // .andDo(print())
  .andExpect(content().contentType(MediaType.APPLICATION_JSON_VALUE))
  .andExpect(status().isOk());