Win a copy of Succeeding with AI this week in the Artificial Intelligence and Machine Learning 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
  • Junilu Lacar
Sheriffs:
  • Tim Cooke
  • Jeanne Boyarsky
  • Knute Snortum
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:
  • salvin francis
  • fred rosenberger
  • Frits Walraven

MockMVC - 415 Error

 
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • 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>
 
This is my favorite tiny ad:
Two software engineers solve most of the world's problems in one K&R sized book
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic