2017-12-11 19 views
2

deleteメソッドを含むコントローラメソッドをテストする必要があります。ここでは、部分的コントローラーのコードは次のとおりです。春MockMvc - RESTコントローラの削除要求をテストする方法は?

@RestController 
@RequestMapping("/api/foo") 
public class FooController { 

    @Autowired 
    private FooService fooService; 

    // other methods which works fine in tests 

    @RequestMapping(path="/{id}", method = RequestMethod.DELETE) 
    public void delete(@PathVariable Long id) { 
     fooService.delete(id); 
    }  
} 

そしてここでは、私のテストです。失敗と

@InjectMocks 
private FooController fooController; 

@Before 
public void setUp() { 
    this.mockMvc = MockMvcBuilders.standaloneSetup(fooController) 
.setControllerAdvice(new ExceptionHandler()).alwaysExpect(MockMvcResultMatchers.content().contentType("application/json;charset=UTF-8")).build(); 
} 

@Test 
public void testFooDelete() throws Exception { 
    this.mockMvc.perform(MockMvcRequestBuilders 
      .delete("/api/foo") 
      .param("id", "11") 
      .contentType(MediaType.APPLICATION_JSON)) 
      .accept(MediaType.APPLICATION_JSON)) 
      .andExpect(status().isOk()); 
} 

テスト仕上げ間違ったステータスコードので:私もこれを見つけたコンソールログに

java.lang.AssertionError: Status Expected :200 Actual :400

2017-12-11 20:11:01 [main] DEBUG o.s.t.w.s.TestDispatcherServlet - DispatcherServlet with name '' processing DELETE request for [/api/foo] 
2017-12-11 20:11:01 [main] DEBUG o.s.w.s.m.m.a.RequestMappingHandlerMapping - Looking up handler method for path /api/foo 
2017-12-11 20:11:01 [main] DEBUG o.s.w.s.m.m.a.ExceptionHandlerExceptionResolver - Resolving exception from handler [null]: org.springframework.web.HttpRequestMethodNotSupportedException: Request method 'DELETE' not supported 
2017-12-11 20:11:01 [main] DEBUG o.s.w.s.m.m.a.ExceptionHandlerExceptionResolver - Invoking @ExceptionHandler method: public org.springframework.http.ResponseEntity<java.util.Map<java.lang.String, java.lang.Object>> cz.ita.javaee.web.controller.error.ExceptionHandler.handleException(java.lang.Exception) 
2017-12-11 20:11:01 [main] DEBUG o.s.w.s.m.m.a.HttpEntityMethodProcessor - Written [{stackTrace=org.springframework.web.HttpRequestMethodNotSupportedException: Request method 'DELETE' not supported 

それを修正する方法を教えてもらえますか?ありがとう。

+1

私はのparam()メソッドは、APIそれを持っているように、パス変数を置くことと等価であるとは思いません、しかし、私は確信していません。言い換えれば、テストがやっている呼び出しは、/ api/foo/11の代わりに/ api/foo?id = 11と思われます。しかし、これは単なる推測です。 – AHungerArtist

+0

@AHungerArtist私はそれを変更しましたが、同じエラーです。 –

答えて

2

対象のURIは/api/foo/11です。このルートに基づく:/api/fooとこのパス変数:/{id}です。

MockMvcを使用する場合は、あなたがそうのようなパス変数(別名URI変数)を設定:

delete(uri, uriVars) 

詳細in the Javadocsを。

だから、あなたのテストは次のようになります。

@Test 
public void testFooDelete() throws Exception { 
    this.mockMvc.perform(MockMvcRequestBuilders 
      .delete("/api/foo/{id}", "11") 
      .contentType(MediaType.APPLICATION_JSON)) 
      .accept(MediaType.APPLICATION_JSON)) 
      .andExpect(status().isOk()); 
} 

または代わりに:

@Test 
public void testFooDelete() throws Exception { 
    this.mockMvc.perform(MockMvcRequestBuilders 
      .delete("/api/foo/11") 
      .contentType(MediaType.APPLICATION_JSON)) 
      .accept(MediaType.APPLICATION_JSON)) 
      .andExpect(status().isOk()); 
} 
+0

それは動作します:)ありがとう。 –

関連する問題