I use spring-boot 2.0.3.RELEASE with junit5. I have just tried to test multipart request with mockMvc.
MockMultipartFile file = new MockMultipartFile("file", "contract.pdf", MediaType.APPLICATION_PDF_VALUE, "<<pdf data>>".getBytes(StandardCharsets.UTF_8));
MockMultipartFile metadata = new MockMultipartFile("metadata", "", MediaType.APPLICATION_JSON_VALUE, objectMapper.writeValueAsString(metadata).getBytes(StandardCharsets.UTF_8));
this.mockMvc.perform(multipart("/documents")
.file(file)
.file(metadata))
.andExpect(status().isNoContent());
But it is not working with part metadata (json).
I always get exception
org.springframework.web.util.NestedServletException: Request processing failed; nested exception is java.lang.IllegalArgumentException: originalFilename is required.
What is wrong?
EDIT:
My implementation is in spring integration dsl. I think that exception is invoked in org.springframework.integration.http.multipart.UploadedMultipartFile
. How can I test multipart request with mockMvc?
@Bean
public IntegrationFlow multipartForm() {
return IntegrationFlows.from(Http.inboundChannelAdapter("/documents")
.statusCodeExpression(HttpStatus.NO_CONTENT.toString())
.requestMapping(m -> m
.methods(HttpMethod.POST)
.consumes(MediaType.MULTIPART_FORM_DATA_VALUE)
))
.handle(new MultipartReceiver())
.get();
}
My demo project is in github https://github.com/bulalak/demo-spring-integration-http
You have not shared your controller so I writing here an example from mine.
Here is a test with MultipartFile in spring boot :
User controller :
UserCreateRequest :