I can't figure out why my List documents is always null
That's Controller code
@PostMapping(value = "/{documentType}/list", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
public ResponseEntity<Void> uploadListOfDocuments(@ApiParam(hidden = true) @RequestAttribute(SharedConstants.USER_ID_ATTRIBUTE) Long userId,
@PathVariable DocumentType documentType,
@RequestBody List<MultipartFile> documents) {
documentGatewayService.uploadListOfDocuments(documents, documentType, userId);
return ResponseEntity.ok().build();
}
That's integration test
@Test
void uploadListOfDocuments() throws Exception {
MockMultipartFile firstFile = new MockMultipartFile("data", "filename.txt", String.valueOf(MediaType.MULTIPART_FORM_DATA), "some xml".getBytes());
MockMultipartFile secondFile = new MockMultipartFile("data", "other-file-name.data", String.valueOf(MediaType.MULTIPART_FORM_DATA), "some other type".getBytes());
MockMultipartFile jsonFile = new MockMultipartFile("json", "", String.valueOf(MediaType.MULTIPART_FORM_DATA), "{\"json\": \"someValue\"}".getBytes());
mockMvc.perform(
multipart("/document/{documentType}/list", "PASSPORT")
.file(firstFile)
.file(secondFile)
.file(jsonFile)
.content(firstFile.getBytes())
.content(secondFile.getBytes())
.content(jsonFile.getBytes())
.requestAttr(SharedConstants.USER_ID_ATTRIBUTE, 1L)
)
.andExpect(status().isOk());
}
I tried many options, but still got null. I would be grateful if anyone could help me figure it out.
The names of MockMultipartFile
objects at your tests have to be equal to the name of List<MultipartFile>
request parameter
In your case they should look like
MockMultipartFile firstFile = new MockMultipartFile("documents", "filename.txt", String.valueOf(MediaType.MULTIPART_FORM_DATA), "some xml".getBytes());
MockMultipartFile secondFile = new MockMultipartFile("documents", "other-file-name.data", String.valueOf(MediaType.MULTIPART_FORM_DATA), "some other type".getBytes());
// ...