测试一个spring文件上传表单,controlelr签名如下所示
@RequestMapping(value = "upload", method = RequestMethod.POST)
@ResponseBody
public void upload(@RequestParam("file") MultipartFile multipartFile) {}
并测试此
final MockMultipartFile file
= new MockMultipartFile("content", "myFile.txt", "text/plain", "hello".getBytes());
MockHttpServletRequestBuilder mockHttpServletRequestBuilder =
.fileUpload("/upload/")
.file(file)
.accept(MediaType.APPLICATION_JSON);
但我得到上述内容:错误消息=必需的MultipartFile参数'file'不存在
答案 0 :(得分:8)
您将参数命名为“file”而不是“content”:
变化:
new MockMultipartFile("content", "myFile.txt", "text/plain", "hello".getBytes());
要:
new MockMultipartFile("file", "myFile.txt", "text/plain", "hello".getBytes());