In my code I am doing something like:

mockMvc.multipart(DOCUMENTS, uuid) {
      file(MockMultipartFile("file", "test.txt", "text/plain", "Test".toByteArray()))
      part(MockPart("data", """{"node":"node"}""".toByteArray()))
}

which is supposed to create a request with both a part, named data, and a file, named file.

This was supported in Spring 5.2, but it is not supported anymore in Spring 5.3 since https://github.com/spring-projects/spring-framework/issues/25602

I am not sure if it was a "wrong" use case, not meant to be working, or if the fix introduced in the linked PR is a regression, since TestDispatcherServlet simply creates a StandardMultipartHttpServletRequest without copying the files over, as MockMultipartHttpServletRequestBuilder was originally doing.

https://github.com/spring-projects/spring-framework/blob/master/spring-web/src/main/java/org/springframework/web/multipart/support/StandardMultipartHttpServletRequest.java#L95 simply copies the parts, but not the files

Comment From: rstoyanchev

Can you provide more details around the failure and the target controller method? Also have you tried providing the file as a part, something like this (headers can also be set via setters):

part(MockPart("file", "test.txt", "Test".toByteArray()))
part(MockPart("data", """{"node":"node"}""".toByteArray()))

Comment From: ThanksForAllTheFish

@rstoyanchev yes, I indeed tried with multiple MockParts and that passes to my controller method the expected values. I could definitely use MockPart instead of MockMultipartFile.

As for the controller method I expect to be invoked:

@PostMapping(consumes = [MediaType.MULTIPART_FORM_DATA_VALUE, MediaType.APPLICATION_JSON_VALUE])
fun uploadDocuments(
    @RequestPart("file") file: MultipartFile
)

this is it. Since https://github.com/spring-projects/spring-framework/issues/25602, file is null when both part and file are used in the test.

Just to be clear, it is absolutely not a problem to use MockPart, it just felt weird that the two can be combined but then file ends up being discarded and wanted to double check if the change in the PR really led to the wanted result.

Comment From: rstoyanchev

Indeed, this was not intended. In MockMultipartHttpServletRequestBuilder, when both files and parts are registered, we can turn the files to parts with a filename and effectively register everything as parts.