title: java.lang.AssertionError: Content type not set - Spring Controller Junit Tests
body: <p>I am trying to do some unit testing on my controllers.  No matter what I do all controller tests return </p>
  
  <pre><code>java.lang.AssertionError: Content type not set
  </code></pre>
  
  <p>I am testing that the methods return json and xml data.</p>
  
  <p>Here is an example of the controller:</p>
  
  <pre><code>@Controller
  @RequestMapping("/mypath")
  
  public class MyController {
  
     @Autowired
     MyService myService;
  
     @RequestMapping(value="/schema", method = RequestMethod.GET)
     public ResponseEntity&lt;MyObject&gt; getSchema(HttpServletRequest request) {
  
         return new ResponseEntity&lt;MyObject&gt;(new MyObject(), HttpStatus.OK);
  
     }
  
  }
  </code></pre>
  
  <p>The unit test is set up like this:</p>
  
  <pre><code>public class ControllerTest() { 
  
  private static final String path = "/mypath/schema";
  private static final String jsonPath = "$.myObject.val";
  private static final String defaultVal = "HELLO";
  
  MockMvc mockMvc;
  
  @InjectMocks
  MyController controller;
  
  @Mock
  MyService myService;
  
  @Before
  public void setup() {
      MockitoAnnotations.initMocks(this);
  
      mockMvc = standaloneSetup(controller)
                  .setMessageConverters(new MappingJackson2HttpMessageConverter(),
                          new Jaxb2RootElementHttpMessageConverter()).build();
  
  
      when(myService.getInfo(any(String.class))).thenReturn(information);
      when(myService.getInfo(any(String.class), any(Date.class))).thenReturn(informationOld);
  
  }
  
  @Test
  public void pathReturnsJsonData() throws Exception {
  
      mockMvc.perform(get(path).contentType(MediaType.APPLICATION_JSON))
          .andDo(print())
          .andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON))
          .andExpect(jsonPath(jsonPath).value(defaultVal));
  }
  </code></pre>
  
  <p>}</p>
  
  <p>I am using:
  Spring 4.0.2
  Junit 4.11
  Gradle 1.12</p>
  
  <p>I have seen the SO question <a href="https://stackoverflow.com/questions/22183178/java-lang-assertionerror-content-type-not-set-while-junit-spring-mvc-controller">Similiar Question</a> but no matter what combination of contentType and expect in my unit test I get the same result.</p>
  
  <p>Any help would be much appreciated.</p>
  
  <p>Thanks</p>