java.lang.AssertionError: Content type not set - Spring Controller Junit Tests java.lang.AssertionError: Content type not set - Spring Controller Junit Tests spring spring

java.lang.AssertionError: Content type not set - Spring Controller Junit Tests


Your solution depends on what kinds of annotation you want to use in your project.

  • You can add @ResponseBody to your getSchema method in Controller

  • Or, maybe adding produces attribute in your @RequestMapping can solve it too.

    @RequestMapping(value="/schema",       method = RequestMethod.GET,       produces = {MediaType.APPLICATION_JSON_VALUE} )
  • Final choice, add headers to your ResponseEntity (which is one of the main objective of using this class)

    //...HttpHeaders headers = new HttpHeaders();headers.add("Content-Type", "application/json; charset=utf-8");return new ResponseEntity<MyObject>(new MyObject(), headers, HttpStatus.OK);

Edit : I've just seen you want Json AND Xml Data, so the better choice would be the produces attribute:

@RequestMapping(value="/schema",       method = RequestMethod.GET,       produces = {MediaType.APPLICATION_JSON_VALUE, MediaType.APPLICATION_XML_VALUE} )


You need to add

@RequestMapping(produces = MediaType.APPLICATION_JSON_VALUE, method = RequestMethod.GETvalue = "/schema")

And <mvc:annotation-driven />in your xml config or @EnableWebMvc