title: HttpRequestMethodNotSupportedException: Request method 'POST' not supported
body: <p>Creating a unit test with MockMvc I am running into:</p>
  
  <p>HttpRequestMethodNotSupportedException: Request method 'POST' not supported</p>
  
  <p>Which causes the test case to fail expecting a '200' but getting a '405'. Some of the additional things you may see in the Junit are for Spring Rest Docs and can be ignored. Such as the @Rule or the .andDo() on the mockMvc Call. I have followed the following documentation <a href="http://docs.spring.io/autorepo/docs/spring-restdocs/1.0.0.RC1/reference/html5/#documenting-your-api-path-parameters" rel="nofollow">Spring Rest Docs - Path Parameter</a> and cannot seem to get it working.</p>
  
  <p>Here is the Controller:</p>
  
  <pre><code>@RestController("/transferObjects")
  public class TransferObjectController {
  
      @RequestMapping(method=RequestMethod.GET, produces="application/json")
      public List&lt;TransferObject&gt; getTransferObjects(){
          // do some magic
          return null;
      }
  
      @RequestMapping(value = "/{name}", method=RequestMethod.POST)
      @ResponseStatus(HttpStatus.OK)
      public void addTransferObject(@PathVariable("name")String name){
          // do magic
      }
  
      @RequestMapping(value = "/{name}", method=RequestMethod.DELETE)
      @ResponseStatus(HttpStatus.OK)
      public void deleteTransferObject(@PathVariable("name")String name){
          // do magic
      }
  </code></pre>
  
  <p>Here is the Junit Class:</p>
  
  <pre><code>public class TransferObjectControllerTest {
  
  @Rule
  public RestDocumentation restDocumentation = new RestDocumentation("target/generated-snippets");
  
  private MockMvc mockMvc;
  
  @Before
  public void setUp() throws Exception {      
      this.mockMvc = MockMvcBuilders.standaloneSetup(new TransferObjectController())
              .apply(documentationConfiguration(this.restDocumentation))
              .build();
  }
  
  @Test
  public void testAddTransferObject() throws Exception {
      this.mockMvc.perform(post("/transferObjects/{name}", "hi"))
          .andExpect(status().isOk()).andDo(document("transferObject",
                  pathParameters(
                      parameterWithName("name").description("The name of the new Transfer Object to be created."))));
  }
  </code></pre>
  
  <p>And here is the console while running the test:</p>
  
  <pre><code>11:20:48.148 [main] INFO  o.s.w.s.m.m.a.RequestMappingHandlerAdapter - Looking for @ControllerAdvice: org.springframework.test.web.servlet.setup.StubWebApplicationContext@5ab785fe
      11:20:48.205 [main] DEBUG o.s.w.s.m.m.a.ExceptionHandlerExceptionResolver - Looking for exception mappings: org.springframework.test.web.servlet.setup.StubWebApplicationContext@5ab785fe
      11:20:48.283 [main] DEBUG o.s.t.w.s.TestDispatcherServlet - Initializing servlet ''
      11:20:48.307 [main] DEBUG o.s.w.c.s.StandardServletEnvironment - Adding [servletConfigInitParams] PropertySource with lowest search precedence
      11:20:48.307 [main] DEBUG o.s.w.c.s.StandardServletEnvironment - Adding [servletContextInitParams] PropertySource with lowest search precedence
      11:20:48.312 [main] DEBUG o.s.w.c.s.StandardServletEnvironment - Adding [systemProperties] PropertySource with lowest search precedence
      11:20:48.312 [main] DEBUG o.s.w.c.s.StandardServletEnvironment - Adding [systemEnvironment] PropertySource with lowest search precedence
      11:20:48.313 [main] DEBUG o.s.w.c.s.StandardServletEnvironment - Initialized StandardServletEnvironment with PropertySources [servletConfigInitParams,servletContextInitParams,systemProperties,systemEnvironment]
      11:20:48.313 [main] INFO  o.s.mock.web.MockServletContext - Initializing Spring FrameworkServlet ''
      11:20:48.313 [main] INFO  o.s.t.w.s.TestDispatcherServlet - FrameworkServlet '': initialization started
      11:20:48.318 [main] DEBUG o.s.t.w.s.TestDispatcherServlet - Unable to locate MultipartResolver with name 'multipartResolver': no multipart request handling provided
      11:20:48.318 [main] DEBUG o.s.t.w.s.TestDispatcherServlet - Using LocaleResolver [org.springframework.web.servlet.i18n.AcceptHeaderLocaleResolver@63238bf4]
      11:20:48.318 [main] DEBUG o.s.t.w.s.TestDispatcherServlet - Using ThemeResolver [org.springframework.web.servlet.theme.FixedThemeResolver@32b97305]
      11:20:48.319 [main] DEBUG o.s.t.w.s.TestDispatcherServlet - Using RequestToViewNameTranslator [org.springframework.web.servlet.view.DefaultRequestToViewNameTranslator@2d2e6747]
      11:20:48.319 [main] DEBUG o.s.t.w.s.TestDispatcherServlet - Using FlashMapManager [org.springframework.web.servlet.support.SessionFlashMapManager@417e7d7d]
      11:20:48.319 [main] DEBUG o.s.t.w.s.TestDispatcherServlet - Published WebApplicationContext of servlet '' as ServletContext attribute with name [org.springframework.web.servlet.FrameworkServlet.CONTEXT.]
      11:20:48.319 [main] INFO  o.s.t.w.s.TestDispatcherServlet - FrameworkServlet '': initialization completed in 6 ms
      11:20:48.319 [main] DEBUG o.s.t.w.s.TestDispatcherServlet - Servlet '' configured successfully
      11:20:48.361 [main] DEBUG o.s.t.w.s.TestDispatcherServlet - DispatcherServlet with name '' processing POST request for [/transferObjects/hi]
      11:20:48.364 [main] DEBUG o.s.t.w.s.s.StandaloneMockMvcBuilder$StaticRequestMappingHandlerMapping - Looking up handler method for path /transferObjects/hi
      11:20:48.368 [main] DEBUG o.s.w.s.m.m.a.ExceptionHandlerExceptionResolver - Resolving exception from handler [null]: org.springframework.web.HttpRequestMethodNotSupportedException: Request method 'POST' not supported
      11:20:48.369 [main] DEBUG o.s.w.s.m.a.ResponseStatusExceptionResolver - Resolving exception from handler [null]: org.springframework.web.HttpRequestMethodNotSupportedException: Request method 'POST' not supported
      11:20:48.369 [main] DEBUG o.s.w.s.m.s.DefaultHandlerExceptionResolver - Resolving exception from handler [null]: org.springframework.web.HttpRequestMethodNotSupportedException: Request method 'POST' not supported
      11:20:48.369 [main] WARN  o.s.web.servlet.PageNotFound - Request method 'POST' not supported
      11:20:48.370 [main] DEBUG o.s.t.w.s.TestDispatcherServlet - Null ModelAndView returned to DispatcherServlet with name '': assuming HandlerAdapter completed request handling
      11:20:48.370 [main] DEBUG o.s.t.w.s.TestDispatcherServlet - Successfully completed request
  </code></pre>