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 ...
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
  • Junilu Lacar
  • Tim Cooke
  • Jeanne Boyarsky
  • Knute Snortum
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
  • Piet Souris
  • 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");
catch(Exception e) {
return new ResponseEntity<ResponseBody>(responseBody, HttpStatus.OK);

Test : -

public void testRequestBodyInput() throws Exception {

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

MockHttpServletRequestBuilder request = post("/textTranslation");

* mockMvc.perform(post("/textTranslation").
* content(asJsonString(translationRequest)).contentType(MediaType.
* 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
    Bookmark Topic Watch Topic
  • New Topic