title: Spring MVC controller JSON date serialization during testing
body: <p>I'm writing unit tests for Spring MVC 4.2.x REST controllers and have an issue with dates serialized by Jackson (2.6.x). While running the application the dates (<code>java.util.Date</code>) are serialized in the format <code>yyyy-MM-dd</code> by default (no extra configuration), which is what I want.</p>
  
  <p>However during tests the dates are serialized as timestamps for an unknown reason.</p>
  
  <p>Here's some sample code for the test class:</p>
  
  <pre><code>public class OrderControllerTest {
  
      @Mock
      private OrderService service;
  
      @InjectMocks
      private OrderController controller;
  
      private MockMvc mockMvc;
  
      private List&lt;Order&gt; orders = new ArrayList&lt;&gt;();
  
      @Before
      public void setup() {
          MockitoAnnotations.initMocks(this);
          mockMvc = MockMvcBuilders.standaloneSetup(controller).build();
          // Initialize dummy order list
          orders.add(...);
          orders.add(...);
          orders.add(...);
      }
  
      @Test
      public void list() throws Exception {
          // Stub the service find method
          when(service.find(asOfDate, null)).thenReturn(orders);
  
          // Run the controller and check the result
          MvcResult result =
              mockMvc
                  .perform(get("/admin/order/"))
                  .andExpect(status().isOk())
                  .andExpect(content().contentType(TestUtil.APPLICATION_JSON_UTF8))
                  .andExpect(jsonPath("$[0].startDate", is(SOME_DATE)))
                  .andReturn();
  
          System.out.println(result.getResponse().getContentAsString());
      }
  </code></pre>
  
  <p>So here I see that the dates returned are all timestamps.</p>
  
  <p>Surefire output:</p>
  
  <pre><code>Expected: is &lt;Thu Jan 01 00:00:00 CET 2015&gt;
       but: was &lt;1420066800000L&gt;
  </code></pre>
  
  <p>Am I missing something? Why is the default serialization format different for running the application and for running tests?</p>