2017-12-11 7 views
2

Ich muss meine Controller-Methoden einschließlich einer Löschmethode testen. Hier ist teilweise Controller-Code:Spring MockMvc - So testen Sie die Löschanforderung des REST-Controllers?

@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-Finish mit dem Scheitern, da falsche Statuscode:

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

In Konsolenprotokoll ich diese 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 ich es beheben kann? Vielen Dank.

+1

Ich glaube nicht das param() -Methode entspricht Pfadvariablen wie die API es darum hat, aber ich bin mir nicht sicher. Mit anderen Worten, ich denke, der Aufruf, den der Test macht, ist eher wie/api/foo? id = 11 anstelle von/api/foo/11. Das ist nur eine Vermutung. – AHungerArtist

+0

@AHungerArtist Ich habe es aber denselben Fehler geändert. –

Antwort

2

Ihre Ziel-URI ist: /api/foo/11 basierend auf diesem Stamm: /api/foo und diese Pfadvariable: /{id}.

Wenn MockMvc eingestellten Pfadvariablen (auch bekannt als Variablen URI) wie folgt:

delete(uri, uriVars) 

Weitere Details in the Javadocs.

So sollte Ihr Test lesen:

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

Oder alternativ:

@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

es funktioniert :) vielen Dank. –

Verwandte Themen