Home > Blockchain >  upload multipart file springboot mockMvc Required request part 'file' is not present, test
upload multipart file springboot mockMvc Required request part 'file' is not present, test

Time:04-15

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"))
  }

CodePudding user response:

The name of the file should be "file" here

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

  • Related