Frühjahr MockMvc - Wie test-Anfrage löschen von REST-controller?

Brauche ich zum testen meiner controller-Methoden einschließlich der delete-Methode. Hier ist teilweise controller-code:

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

Und hier ist mein test:

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

Test beenden mit Fehler wegen falschen status-code:

java.lang.AssertionError: Status
Erwartet :200 Aktuell :400

Im Konsolen-log habe ich auch gefunden:

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

Können Sie mir sagen, wie es zu lösen ist? Danke.

  • Ich glaube nicht, dass der param () - Methode ist äquivalent zu setzen, eine path-variable, die wie die API hat, aber ich bin mir nicht sicher.In anderen Worten, ich glaube die nennen den test macht, ist mehr wie /api/foo?id=11 statt /api/foo/11. Dies ist nur eine Vermutung, aber.
  • Ich habe es geändert aber gleicher Fehler.
Schreibe einen Kommentar