简体   繁体   中英

Unable to hit the endpoint with MultipartFile array from Junit test case

I am getting 404 instead of 200 response code when I try to hit the endpoint. It is not hitting the endpoint "/multiFilestore" . I think I am not forming the request correctly but could not figure out what is missing here. Any pointers will be greatly appreciated.

My endpoint is like below

@PostMapping(path = "/multiFilesStore", consumes = { MediaType.MULTIPART_FORM_DATA_VALUE })
public Map<String, String> myUploads(@RequestPart(value = "attachments") final MultipartFile[] attachments, @RequestPart(value = "prefix", required = true) String prefix ) {
   return s3Service.uploadFiles(attachments, prefix);
}

My JUnit test case is

public void testMyUploadFiles() throws Exception {
    byte[] content = new byte[100];
    MockMultipartFile filePart1 = new MockMultipartFile("files", "file1.pdf", null, content);
    MockMultipartFile filePart2 = new MockMultipartFile("files", "file2.pdf", null, content);
    MultipartFile[] fileAttachments={filePart1,filePart2};

    Mockito.when(s3Service.uploadFiles(fileAttachments, "dummyFolder")).thenReturn(Map.of("file1.pdf", "Ok"));

    ResultMatcher ok = MockMvcResultMatchers.status().isOk();
    mockMvc.perform(MockMvcRequestBuilders.fileUpload("/multiFilesStore")
                    .file(filePart1)
                    .file(filePart2)
                    .param("prefix", "dummyFolder")
                    .contentType(MediaType.MULTIPART_FORM_DATA_VALUE))
                .andDo(MockMvcResultHandlers.log())
                .andExpect(ok);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM