Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error message = Required MultipartFile parameter 'file' is not present

Testing a spring file upload form, the controlelr signature looks like this

@RequestMapping(value = "upload", method = RequestMethod.POST)
@ResponseBody
public void upload(@RequestParam("file") MultipartFile multipartFile) {}

and the test this

final MockMultipartFile file 
  = new MockMultipartFile("content", "myFile.txt", "text/plain", "hello".getBytes());

MockHttpServletRequestBuilder mockHttpServletRequestBuilder = 
  .fileUpload("/upload/")
  .file(file)
  .accept(MediaType.APPLICATION_JSON);

but I get the aforementioned : Error message = Required MultipartFile parameter 'file' is not present

like image 542
NimChimpsky Avatar asked Dec 26 '22 20:12

NimChimpsky


1 Answers

You named the parameter as "file" and not as "content":

Change:

new MockMultipartFile("content", "myFile.txt", "text/plain", "hello".getBytes());

To:

new MockMultipartFile("file", "myFile.txt", "text/plain", "hello".getBytes());
like image 96
Beto Neto Avatar answered Dec 28 '22 10:12

Beto Neto