Win a copy of Head First Android this week in the Android forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

MockMVC - 415 Error

 
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I trying to hit Spring Controller service using the mockmvc test but every time I am getting "org.springframework.web.HttpMediaTypeNotSupportedException" . I appreciate if any one refer  below details and try to suggest me .

Controller : -
@PostMapping(value="/textTranslation",consumes = MediaType.APPLICATION_JSON_VALUE , produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<ResponseBody>  translateText(@RequestBody TranslationRequest tranaTranslationRequest) throws Exception {
ResponseBody responseBody = new ResponseBody();
try {
System.out.println("In side Controller");
responseBody.setTest("test");
}
catch(Exception e) {
e.printStackTrace();
}
return new ResponseEntity<ResponseBody>(responseBody, HttpStatus.OK);
  }

Test : -

       @Test
public void testRequestBodyInput() throws Exception {

TranslationRequest translationRequest = new TranslationRequest("english","french","test");

MockHttpServletRequestBuilder request = post("/textTranslation");
request.content(asJsonString(translationRequest));
request.accept(MediaType.APPLICATION_JSON_UTF8_VALUE);
request.contentType(MediaType.APPLICATION_JSON_UTF8_VALUE);
mockMvc.perform(request)
   .andDo(print())
   .andExpect(status().isOk());

/*
* mockMvc.perform(post("/textTranslation").
* content(asJsonString(translationRequest)).contentType(MediaType.
* APPLICATION_JSON_UTF8_VALUE).accept(MediaType.APPLICATION_JSON_UTF8_VALUE)).
* andDo(print()) .andExpect(status().isOk()).
* andExpect(MockMvcResultMatchers.jsonPath("$.test").value("test"));
*/

}


    public static String asJsonString(final Object obj) {
   try {
       return new ObjectMapper().writeValueAsString(obj);
   } catch (Exception e) {
       throw new RuntimeException(e);
   }
}

Spring Version - 5.0.1.RELEASE

Test Error - java.lang.AssertionError: Status expected:<200> but was:<415>
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic