title: Unit test MockHttpServletRequest not returning content type
body: <p>I would like the application to return JSON object from my Java Classes (both success and fail cases). </p>
  
  <p>I had defined a <code>@RestControllerAdvice</code> to handle the errors from controller. My program also shows the error message correctly in json, but the problem is in the <strong>unit test</strong>.</p>
  
  <p>The problem is when it throws:</p>
  
  <p><code>org.springframework.web.bind.MethodArgumentNotValidException</code></p>
  
  <p>My unit test failed with error :</p>
  
  <p><code>java.lang.AssertionError: Response header 'content-type' expected:&lt;application/json;charset=UTF-8&gt; but was:&lt;null&gt;</code></p>
  
  <p><strong>Controller:</strong></p>
  
  <pre><code>@PostMapping("/import")
  public ResponseEntity&lt;StatusModel&gt; import(@Valid @RequestBody ImportModel importModel ){
      //logic
      return new ResponseEntity&lt;&gt;(new StatusModel("Data accepted."), HttpStatus.OK);
  
  }
  </code></pre>
  
  <p><strong>Unit Test:</strong></p>
  
  <pre><code>@RunWith(SpringJUnit4ClassRunner.class)
  @ContextConfiguration(classes = {MockConfiguration.class})
  @WebAppConfiguration
  public class ModelControllerTest {
  
      private MockMvc mockMvc;
  
      @InjectMocks
      private ModelController controller;
  
      @Before
      public void setUp() {
          mockMvc = MockMvcBuilders.standaloneSetup(controller).build();
      }
  
      @Test
      public void import_validRequest_imported() throws Exception {
  
          mockMvc
              .perform(
                  post("/import")
                      .content(VALID_CONTENT).contentType("application/json;charset=UTF-8"))
              .andExpect(status().isOk())
              .andExpect(header().string("content-type", "application/json;charset=UTF-8"))
              .andExpect(jsonPath("$.status", equalTo("Data accepted")));
      }
  
      @Test
      public void import_invalidRequest_notImported() throws Exception {    
          mockMvc
              .perform(
                  post("/import")
                      .content(INVALID_CONTENT).contentType("application/json"))
              .andExpect(status().isBadRequest())
              .andDo(print())
              .andExpect(header().string("content-type", "application/json"));  &lt;----- This assertion failed
      }   
  }
  </code></pre>
  
  <p><strong>MockHttpServletRequest log:</strong></p>
  
  <pre><code>MockHttpServletRequest:
        HTTP Method = POST
        Request URI = /import
         Parameters = {}
            Headers = {Content-Type=[application/json]}
  
  Handler:
               Type = com.test.ModelController
             Method = public org.springframework.http.ResponseEntity&lt;com.model.StatusModel&gt; com.ModelController.import(com.test.model.ImportModel)
  
  Async:
      Async started = false
       Async result = null
  
  Resolved Exception:
               Type = org.springframework.web.bind.MethodArgumentNotValidException
  
  ModelAndView:
          View name = null
               View = null
              Model = null
  
  FlashMap:
         Attributes = null
  
  MockHttpServletResponse:
             Status = 400
      Error message = null
            Headers = {}
       Content type = null
               Body = 
      Forwarded URL = null
     Redirected URL = null
            Cookies = []
  </code></pre>
  
  <p>Why is the content type, error message is empty?</p>