【发布时间】:2020-07-10 16:06:47
【问题描述】:
当我只测试微不足道的 POST 请求时,我可以获取 MvcResult 并从中获取一些东西:
MvcResult result = mockMvc.perform(post("/api/register")
.contentType(MEDIA_TYPE_JSON_UTF8)
.content(new Gson().toJson(request)))
.andExpect(status().isCreated())
.andReturn();
Long tempId = Long.valueOf(JsonPath.read(result.getResponse().getContentAsString(), "$.id").toString());
但是当我使用 MultipartFile 时,我只能使用 MockMvcRequestBuilders 并且只能检查预期。
MockMultipartFile filePart = new MockMultipartFile(
"file",
"file.jpg",
"image/jpeg", file);
mockMvc.perform(MockMvcRequestBuilders.multipart("/api/loadfile")
.file(filePart)
.param("json", json))
.andExpect(status().isOk());
如何在请求后通过多部分 POST 获取 MvcResult?
【问题讨论】:
标签: spring-boot multipart spring-test mockmvc