2017-08-29 9 views
0

私はjUnitテストを初めて使っています。スタックからそれは私の問題を解決しませんでした。春のjUnitテストから@RequestBodyを呼び出す

私のテスト・コントローラ:

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

} 

と私がテストしたい方法:

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

は、私はこのエラーを取得する:Failed to read HTTP message: org.springframework.http.converter.HttpMessageNotReadableException: Required request body is missing。だからどうすればjunit@RequestBody

答えて

1

から呼び出すことができますが、 EmployeeRequestタイプのリクエストボディを追加する必要があります。

EmployeeRequest employeeRequest = new EmployeeRequest(...); 
String body = (new ObjectMapper()).valueToTree(employeeRequest).toString(); 
mockMvc.perform(post("http://localhost:8081/requests/add")) 
     .content(body) 
     .andExpect ... 
関連する問題