2017-09-23 83 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