2017-09-23 19 views
0

我想將輸入文件傳遞給MockMVC執行語句。請從下面的代碼片段:MockMVC |需要通過JSON文件作爲輸入

@Test 
public void test() throws Exception { 

    this.mockMvc.perform(post("/tax_rates/v1/quotations") 
      .contentType(MediaType.APPLICATION_JSON_UTF8).pathInfo("/src/main/resources/input.json")) 
      .andExpect((ResultMatcher) status().is2xxSuccessful()); 

} 

當我試圖使用PATHINFO變量,我得到了如下錯誤:

HttpMessageNotReadableException:必需請求體丟失:

我猜它意味着有效載荷沒有通過?

任何建議會幫助我。

的問候,蘇尼爾

回答

1

我們可以通過JSON輸入的內容:

ObjectMapper mapper=new ObjectMapper(); 
String jsonString=mapperwriteValueAsString(mapper.readValue(new File("path/to/file",Object.class)); 
this.mockMvc.perform(post("/tax_rates/v1/quotations") 
      .contentType(MediaType.APPLICATION_JSON_UTF8).content(jsonString)) 
      .andExpect(status().is2xxSuccessful()); 

如果你想通過MultipartFile作爲輸入。這裏是鏈接:

Using Spring MVC Test to unit test multipart POST request