简体   繁体   中英

MockMvc losing file during multipart call

I'm trying to test a file upload using MockMvc, here is the code snippet:

        final Map<String, String> boundary = new HashMap<>() {{ put("boundary", "1000000"); }};
        final MockMultipartFile file = new MockMultipartFile("name", "name.ext", "multipart/form-data", "some data".getBytes());

        mockMvc.perform(
                multipart("/path/to/request")
                .file(file)
                .contentType(new MediaType("multipart", "form-data", boundary))
            ).andExpect(status().isOk())

The call goes through. The method processing the request has only one parameter - a MultipartFile . During a real execution, the method receives the file, no problem. However, when running the mock code above, the method always receives a null - the mock file is getting lost somewhere between the perform(...) and the actual method call.

Any help is appreciated.

The issue was with the file name. It shouldn't be "name", it should have been "file", since this is what the caller was expecting.

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