2016-01-25 306 views
2

創建具有MockMvc我一個單元測試正在到:HttpRequestMethodNotSupportedException:請求方法「POST」不支持

HttpRequestMethodNotSupportedException:請求方法「POST」不支持

導致測試案例失敗的期待「 200',但得到'405'。您在Junit中可能看到的其他一些內容是Spring Rest Docs,可以忽略。如mockMvc Call上的@Rule或.andDo()。我遵循以下文檔Spring Rest Docs - Path Parameter,似乎無法讓它工作。

這裏是控制器:

@RestController("/transferObjects") 
public class TransferObjectController { 

    @RequestMapping(method=RequestMethod.GET, produces="application/json") 
    public List<TransferObject> 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 
    } 

這裏是JUnit類:

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.")))); 
} 

這裏是控制檯運行測試時:

11:20:48.148 [main] INFO o.s.w.s.m.m.a.RequestMappingHandlerAdapter - Looking for @ControllerAdvice: org.s[email protected]5ab785fe 
    11:20:48.205 [main] DEBUG o.s.w.s.m.m.a.ExceptionHandlerExceptionResolver - Looking for exception mappings: org.s[email protected]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 [[email protected]63238bf4] 
    11:20:48.318 [main] DEBUG o.s.t.w.s.TestDispatcherServlet - Using ThemeResolver [[email protected]5] 
    11:20:48.319 [main] DEBUG o.s.t.w.s.TestDispatcherServlet - Using RequestToViewNameTranslator [org.spri[email protected]2d2e6747] 
    11:20:48.319 [main] DEBUG o.s.t.w.s.TestDispatcherServlet - Using FlashMapManager [[email protected]17e7d7d] 
    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 
+0

你有春天的安全性? –

+0

未啓用彈簧安全功能 – bhcmoney

回答

1

看來我能夠解決這個問題。

以在控制檯細看運行我注意到測試而:

Mapped "{[],methods=[GET],produces=[application/json]}" onto public java.util.List<common.to.TransferObject> sf.common.controller.TransferObjectController.getTransferObjects() 
Mapped "{[/{name}],methods=[POST]}" onto public void sf.common.controller.TransferObjectController.addTransferObject(java.lang.String) 
Mapped "{[/{name}],methods=[DELETE]}" onto public void sf.common.controller.TransferObjectController.deleteTransferObject(java.lang.String) 

這表明,它是映射控制器請求映射到特定RequestMapping方法成立。 @RestController("/transferObjects")實際上並未將映射定義爲父項。對於我來說,我必須在@RestController下包含@RequestMapping("/transferObjects")

因此,通過改變父映射我現在可以使用下面的測試案例:

@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.")))); 
} 
相關問題