2017-08-29 4 views
0

Ich bin neu in jUnit test.I haben eine Anfrage, ein Verfahren zu testen, die ein @RequestBody object.Searched von Goodle für fast 4 Stunden haben, kann aber nicht finden it.I ähnliche Frage auch gelesen von Stapel aber gelöst das tut mein Problem.von JUnit-Test im Frühjahr Aufruf @RequestBody

Mein Test-Controller:

@Test 
public void testInsertRequest() throws Exception { 
    mockMvc.perform(post("http://localhost:8081/requests/add")) 
      .andExpect(jsonPath("$.date").value("2017-08-09")).andExpect(jsonPath("$.orderPerson").value("Nermin")) 
      .andExpect(jsonPath("$.orderPhone").value("0777675432")).andExpect(jsonPath("$.client").value("Nezrin")) 
      .andExpect(jsonPath("$.clientPhone").value("0776763254")).andExpect(jsonPath("$.department").value("IT")) 
      .andExpect(jsonPath("$.route").value("Neriman Nerimanov")); 

} 

Und Methode, die ich testen wollen:

@RequestMapping(value = "/add", method = RequestMethod.POST) 
public String insertRequest(@RequestBody EmployeeRequest employeeRequest) { 
    System.out.println(employeeRequest.getDate()); 
    requestService.insertRequest(employeeRequest); 
} 

ich diesen Fehler: Failed to read HTTP message: org.springframework.http.converter.HttpMessageNotReadableException: Required request body is missing .So wie kann ich von junit nennen @RequestBody

Antwort

1

Sie sollte eine Anfrage Körper des EmployeeRequest Typ.

EmployeeRequest employeeRequest = new EmployeeRequest(...); 
String body = (new ObjectMapper()).valueToTree(employeeRequest).toString(); 
mockMvc.perform(post("http://localhost:8081/requests/add")) 
     .content(body) 
     .andExpect ... 
Verwandte Themen