0

With Spring Boot 2.3 I was using the following Kotlin code

val mvcResultImage = this.mockMvc!!.perform(MockMvcRequestBuilders.multipart("/somepath)
        .file("files[]", imageFile.getBytes())
        .characterEncoding("UTF-8"))
        .andReturn()

in an integration test for a controller with a function

@PostMapping(path = ["/somepath"],
    consumes = [MediaType.MULTIPART_FORM_DATA_VALUE],
    produces = [MediaType.APPLICATION_JSON_VALUE])
@ResponseBody
fun createFromBytes(@RequestParam("files[]") file: MultipartFile): ResponseEntity<Any> {
   ...
}

In 2.3 I was able to handle the request in the controller function whereas in 2.4 the controller function raises a org.springframework.web.multipart.support.MissingServletRequestPartException with the message Required request part 'files[]' is not present and causes HTTP response code 400.

I don't find anything in the migration guide and list of handled issues for this version change.

A rename to file in both controller and request doesn't help, I don't remember why I added [] in the code working with 2.3, but I think it was necessary to make it work.

I'm using Spring Boot through the maven parent mechanism with spring-boot-starter-parent:2.4.1.

Kalle Richter
  • 8,008
  • 26
  • 77
  • 177

1 Answers1

0

This is a known issues in Spring Boot coming from Spring. It's fixed in Spring Boot 2.4.2. The linked issue contains a successfully tested workaround in case you're stuck with 2.4.1: Create MockMultipartFile with MockMultipartFile( String name, @Nullable String originalFilename, @Nullable String contentType, @Nullable byte[] content) (specification of originalFilename matters).

Kalle Richter
  • 8,008
  • 26
  • 77
  • 177