Spring boot MockMVC |需要将JSON文件作为输入传递

Spring boot MockMVC |需要将JSON文件作为输入传递,spring-boot,mockito,Spring Boot,Mockito,我想将一个输入文件传递给MockMVC perform语句。请在下面查找代码段: @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"))

我想将一个输入文件传递给MockMVC perform语句。请在下面查找代码段:

@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变量时,得到如下错误:

HttpMessageNodeTableException:缺少必需的请求正文:

我猜这意味着有效载荷没有通过

任何建议都会对我有帮助

问候,,
Sunil

我们可以将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作为输入传递。以下是链接: