Integration test script and file upload

I wrote some code for the file related download using spring, it works fine, now I am writing tests for integration, but I ran into some problem My methdd controller

@RequestMapping(value = "/{attributeName}/upload", method = RequestMethod.POST)
@ResponseBody
public Result uploadCompany(HttpServletRequest request,
        @RequestParam MultipartFile file, @PathVariable String attributeName,
        @RequestParam long dateKey)
        throws IOException, PromotionException {
    some code
}

test cases

@Test
public void shouldReturnTrueStatusWhenUploadCompany() throws Exception {
    MockMultipartFile file = new MockMultipartFile("company_upload", "company_upload.csv",
            MediaType.MULTIPART_FORM_DATA_VALUE, EMPLOYEE_NUMBER_FILE_CONTENT.getBytes(UTF_8));
    mockMvc.perform(
            MockMvcRequestBuilders.fileUpload(
                    PROMOTION + StringUtils.replace(ATTRIBUTE_NAME, "{attributeName}", "COMPANY") + "/upload")
                    .file(file).param("dateKey", "852017")  .contentType(MediaType.MULTIPART_FORM_DATA)
                    .accept(MediaType.APPLICATION_JSON))
            .andExpect(status().isOk());

}

but i get

2017-05-09 13:42:42,506 ERROR [Test worker] INTERNAL_SERVER_ERROR: 
org.springframework.web.bind.MissingServletRequestParameterException: Required MultipartFile parameter 'file' is not present

Where am I mistaken?

0
source share
1 answer

change your line

 MockMultipartFile file = new MockMultipartFile("company_upload", "company_upload.csv",
                    MediaType.MULTIPART_FORM_DATA_VALUE, EMPLOYEE_NUMBER_FILE_CONTENT.getBytes(UTF_8));  

to

 MockMultipartFile file = new MockMultipartFile("file", "company_upload.csv",
                    MediaType.MULTIPART_FORM_DATA_VALUE, EMPLOYEE_NUMBER_FILE_CONTENT.getBytes(UTF_8));

or change the declaration of the controller method to something like this

 public Result uploadCompany(HttpServletRequest request,
                @RequestParam(value = "company_upload") MultipartFile file, @PathVariable String attributeName,
                @RequestParam long dateKey)
+4
source

Source: https://habr.com/ru/post/1017211/


All Articles