2015-11-16 3 views
4

Я получаю следующее исключение org.springframework.web.HttpMediaTypeNotSupportedException пытается проверить Json Controller.MockMvc Test Spring бросает org.springframework.web.HttpMediaTypeNotSupportedException

Метод в контроллере:

@RequestMapping(value = "/report", method = RequestMethod.PUT) 
    public @ResponseBody DatosJsonVO report(@RequestHeader(value = "hash", required = true) String hash, 
      @RequestBody ReportVO report) { 
} 

Мой метод испытания заключается в следующем:

@Test 
    public void reportarPreguntaSesionInvalida() throws Exception { 
     ReportVO report = new ReportVO(); 
     report.setIdQuestion(1); 
     report.setIssue("Wrong answer"); 
     mockMvc.perform(put("/json/report").header("hash", "123456789") 
       .accept(MediaType.APPLICATION_JSON).content(asJsonString(report))).andDo(print()) 
       .andExpect(content().string("{\"code\":2,\"message\":\"Session error\",\"data\":null}")); 
    } 

Но, я получаю такой ответ:

MockHttpServletRequest: 
     HTTP Method = PUT 
     Request URI = /json/report 
      Parameters = {} 
      Headers = {hash=[8.16615469E8], Accept=[application/json]} 
      Handler: 
       Type = com.controller.json.QuestionsJsonController 
       Async: 
    Was async started = false 
     Async result = null 
    Resolved Exception: 
       Type = org.springframework.web.HttpMediaTypeNotSupportedException 
     ModelAndView: 
      View name = null 
       View = null 
       Model = null 
      FlashMap: 
MockHttpServletResponse: 
       Status = 415 
     Error message = null 
      Headers = {} 
     Content type = null 
       Body = 
     Forwarded URL = null 
     Redirected URL = null 
      Cookies = [] 

My Spring версии 4.0 .0.RELEASE

+2

Ответил здесь: http://stackoverflow.com/questions/31609496/httpmediatypenotsupportedexception-when-trying-to-test-handling-of-http-post –

+0

Возможный дубликат [HttpMediaTypeNotSupportedException при попытке проверить управляемость HTTP POST] (http://stackoverflow.com/questions/31609496/httpmediatypenotsupportedexception-when-trying-to-test-handling-of-http-post) – sebadagostino

ответ

0

mockMvc.perform (put ("/ json/report"). Header ("hash", "123456789") .accept (MediaType.APPLICATION_JSON) .content (asJsonString (report))). ContentType (MediaType.APPLICATION_JSON). andDo (print()) .andExpect (content(). string ("{\" code \ ": 2, \" message \ ": \" Ошибка сеанса \ ", \" data \ ": null}")) ;

+0

Не могли бы вы дать некоторые детали вашему коду, тем более, что это так нечитаемый? Кроме того, включите ваш код в обратные тики \ 'like this \', чтобы отобразить их 'like this', чтобы он был читабельным. Кроме того, вы можете использовать четыре пробела перед вашей линией для визуализации как кода, что полезно для многострочного кода. –

0

Вам необходимо установить тип содержимого.

mockMvc.perform(put("/json/report") 
.header("hash", "123456789") 
.accept(MediaType.APPLICATION_JSON) 
.contentType(MediaType.APPLICATION_JSON_VALUE) 
.content(asJsonString(report))).andDo(print()) 
.andExpect(content().string("{\"code\":2,\"message\":\"Session error\",\"data\":null}")); 
Смежные вопросы