title: How to pass date objects as request parameters through Spring MockMvc
body: <p>I have a unit test that uses Spring's MockMvc testing framework to hit a REST endpoint. The rest endpoint requires that two string path variables be sent, along with two <code>java.util.Date</code> objects which are passed as <strong>@RequestParameters</strong>. When I run the test, it fails because the Date objects are either not present, or can't be serialized from a string JSON representation to a Date object. For the record, execution fails before hitting the end point listed in the <code>.perform()</code>, so this problem is not coming from the code being tested. It's coming from the test.</p>
  
  <p>Here's my test:</p>
  
  <pre><code>    @Test
      public void testGetHitsForCell() {
      String categoryName = "categoryName";
  
      try {
  
          String startDateJson = gson.toJson(new Date());
          String endDateJson = gson.toJson(new Date());
          ResultActions ra = mvc.perform(MockMvcRequestBuilders.get("/rest/tickets/" + ticketName + "/" + categoryName).requestAttr("startDate", new Date())
                  .requestAttr("endDate", new Date()).with(user(user)));
          MvcResult aResult = ra.andReturn();
          MockHttpServletResponse response = aResult.getResponse();
  
          assertTrue(response.getContentType().equals("application/json;charset=UTF-8"));
          assertTrue(gson.fromJson(response.getContentAsString(), PaginatedResults.class) instanceof PaginatedResults);
      } catch (Exception e) {
          e.printStackTrace();
          fail(e.getMessage());
      }
  </code></pre>
  
  <p>The endpoint looks like this:</p>
  
  <pre><code>/rest/ticket/{ticketName}/{categoryName}?startDate=2015-07-21T14%3A26%3A51.972-0400&amp;endDate=2015-08-04T14%3A26%3A51.972-0400
  </code></pre>
  
  <p>This is the exception that the MockMvc throws:</p>
  
  <blockquote>
    <p>org.springframework.web.bind.MissingServletRequestParameterException: Required Date parameter 'startDate' is not present</p>
  </blockquote>
  
  <p>My test application context looks like this:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8" ?&gt;
  &lt;beans xmlns="http://www.springframework.org/schema/beans"
      xmlns:context="http://www.springframework.org/schema/context"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
      xmlns:util="http://www.springframework.org/schema/util"
      xsi:schemaLocation="
  http://www.springframework.org/schema/beans
  http://www.springframework.org/schema/beans/spring-beans.xsd
  http://www.springframework.org/schema/context
  http://www.springframework.org/schema/context/spring-context.xsd
  http://www.springframework.org/schema/mvc
  http://www.springframework.org/schema/mvc/spring-mvc.xsd
  http://www.springframework.org/schema/util
  http://www.springframework.org/schema/util/spring-util.xsd"&gt;
  
  
      &lt;import resource="classpath:propertiesPlaceholder.xml" /&gt;
      &lt;import resource="classpath:cache.xml" /&gt;
      &lt;import resource="classpath:mongo-context.xml" /&gt;
      &lt;import resource="classpath:securityContext.xml" /&gt;
      &lt;import resource="classpath:service-commons.xml" /&gt;
  
      &lt;import resource="classpath:mockUserProviderContext.xml" /&gt;
  
      &lt;!-- Enables the Spring MVC @Controller programming model --&gt;
      &lt;mvc:annotation-driven&gt;
          &lt;mvc:message-converters register-defaults="true"&gt;
              &lt;!-- Use the Jackson mapper defined above to serialize dates appropriately --&gt;
              &lt;bean
                  class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"&gt;
                  &lt;property name="objectMapper" ref="jacksonObjectMapper" /&gt;
              &lt;/bean&gt;
          &lt;/mvc:message-converters&gt;
      &lt;/mvc:annotation-driven&gt;
  
      &lt;mvc:default-servlet-handler/&gt;
      &lt;!-- Jackson Mapper --&gt;
      &lt;bean name="jacksonObjectMapper"
          class="org.springframework.http.converter.json.Jackson2ObjectMapperFactoryBean"&gt;
          &lt;property name="featuresToDisable"&gt;
              &lt;array&gt;
                  &lt;util:constant
                      static-field="com.fasterxml.jackson.databind.SerializationFeature.WRITE_DATES_AS_TIMESTAMPS" /&gt;
                  &lt;util:constant
                      static-field="com.fasterxml.jackson.databind.SerializationFeature.FAIL_ON_EMPTY_BEANS" /&gt;
              &lt;/array&gt;
          &lt;/property&gt;
      &lt;/bean&gt;
  
  
      &lt;!-- SimpleDateFormat for Jackson to use for formatting Date objects --&gt;
      &lt;bean id="standardDateFormat" class="java.text.SimpleDateFormat"&gt;
          &lt;constructor-arg index="0" value="yyyy-MM-dd'T'HH:mm:ss.SSS'Z'" /&gt;
      &lt;/bean&gt;
  
      &lt;!-- Handles HTTP GET requests for /resources/** by efficiently serving 
          up static resources in the ${webappRoot}/resources directory --&gt;
      &lt;mvc:resources mapping="/resources/**" location="/resources/" /&gt;
  
      &lt;context:component-scan base-package="ticketApp" /&gt;
      &lt;context:component-scan base-package="mongodb.dao.TicketDao" /&gt;
      &lt;context:component-scan base-package="service.commons.*" /&gt;
  
  &lt;/beans&gt;
  </code></pre>
  
  <p>What can I do to correctly pass these Date objects to the above rest endpoint?</p>