Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

upload multipart file springboot mockMvc Required request part 'file' is not present, testing controller

Getting this 'file' not present error when trying to test uploading a file, controller works fine outside testing though. Is there a reason the file isn't present here?

controller

public UploadResponse uploadFile(
    @RequestPart
         MultipartFile file,
     @RequestParam(value = “name”)
         String name) {}

test for controller

MockMultipartFile file
    = new MockMultipartFile(
    "photo.jpeg",
    "photo.jpeg",
    MediaType.IMAGE_JPEG_VALUE,
    "photo".getBytes()
);


this.mockMvc.perform(
            multipart(“/uploadfile”)
                .file(file)
                .param(“name”, “bob”))
        .andDo(print())
        .andExpect(status().isOk())
        .andExpect(content().contentType("application/json"))
  }

like image 880
user3226932 Avatar asked Jan 19 '26 17:01

user3226932


1 Answers

The name of the file should be "file" here

MockMultipartFile file
    = new MockMultipartFile(
    "file",
    "photo.jpeg",
    MediaType.IMAGE_JPEG_VALUE,
    "photo".getBytes()
);

like image 149
user3226932 Avatar answered Jan 22 '26 09:01

user3226932