Skip to content

Required request part 'file' is not present error with MockMvc multipart tests #26265

@sudhirtumati

Description

@sudhirtumati

Spring boot version: 2.4.1

Test:

@Test
@WithMockUser(roles = "APP_OWNER")
void given_application_owner_then_applicationUpload_should_throw_403() throws Exception {
	MockMultipartFile file = new MockMultipartFile("file", "some content".getBytes());
	mockMvc.perform(
			multipart("/applications/actions/upload").file(file).header("Content-Type", "multipart/form-data"))
			.andExpect(status().isForbidden());
}

Controller:

@PostMapping(value = "/actions/upload",
		consumes = { MediaType.MULTIPART_FORM_DATA_VALUE, "multipart/form-data;charset=UTF-8" })
@RolesAllowed("ADMIN")
public List<ApplicationUploadStatusDTO> uploadApplications(@RequestParam("file") MultipartFile file) {
	// Do something here
}

Expected result: 403 error
Actual result: 400

MockHttpServletResponse:
           Status = 400
    Error message = Required request part 'file' is not present
          Headers = [X-Content-Type-Options:"nosniff", X-XSS-Protection:"1; mode=block", Cache-Control:"no-cache, no-store, max-age=0, must-revalidate", Pragma:"no-cache", Expires:"0", X-Frame-Options:"DENY"]
     Content type = null
             Body = 
    Forwarded URL = null
   Redirected URL = null
          Cookies = []

Same code is working as expected with spring-boot 2.4.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions