簡體   English   中英

Spring MockMvc - 如何測試REST控制器的刪除請求?

[英]Spring MockMvc - How to test delete request of REST controller?

我需要測試我的控制器方法,包括刪除方法。 這是部分控制器代碼:

@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:預期狀態:200實際: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

你能告訴我怎么解決嗎? 謝謝。

您的目標URI是: /api/foo/11基於此根: /api/foo和此路徑變量: /{id}

使用MockMvc您可以設置路徑變量(也就是URI變量),如下所示:

delete(uri, uriVars)

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());
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM