title: MockMvc testing OPTIONS request
body: <p>Returning to Java/Spring after many years away.  Trying to develop a controller test. I want test a handler for an OPTIONS request, but when I use MockMvc to do something like</p>
  
  <pre><code>    MockHttpServletRequestBuilder optionsRequest = options("/endpoint/1234");
      optionsRequest.header("Origin", "http://blah");
      this.mockMvc.perform(optionsRequest).
          andExpect(status().isOk()).
          andExpect(header().string("Access-Control-Allow-Credentials", "application/json")).
          andExpect(header().string("Access-Control-Allow-Origin", "application/json")).
          andExpect(header().string("Access-Control-Allow-Methods", "application/json")).
          andExpect(header().string("Access-Control-Allow-Headers", "application/json"));
  </code></pre>
  
  <p>with setup</p>
  
  <pre><code>this.mockMvc = MockMvcBuilders.standaloneSetup(this.controller).build();
  </code></pre>
  
  <p>where <code>controller</code> is an instance of my controller, the options handler never gets fired.</p>
  
  <p>In my server startup, we need to do this</p>
  
  <pre><code>dispatcherServlet.setDispatchOptionsRequest(true);
  </code></pre>
  
  <p>to get the requests handled in the running application.  </p>
  
  <p>Is there a way to access the dispatcher servlet in the test setup so I can set the same option?</p>
  
  <p>I have fallen back to setting up mock request/response objects and invoking the methods manually for now....</p>