Sam Brannen opened SPR-14252 and commented
Status Quo
Spring already has a mock for javax.servlet.http.Part in the test source folder in spring-web; however, there is currently no counterpart in the spring-test module for public consumption.
Further Analysis
As pointed out by a user on Stack Overflow, it is currently not possible to mock Part on your own due to the following:
In
RequestPartServletServerHttpRequestI run into this:
java this.headers = this.multipartRequest.getMultipartHeaders(this.partName); if (this.headers == null) { throw new MissingServletRequestPartException(partName); }
It is therefore likely that we will need to modify MockMultipartHttpServletRequest as well. In particular, the contract for Spring's MultipartHttpServletRequest.getMultipartHeaders() states the following.
Return the headers associated with the specified part of the multipart request.
If the underlying implementation supports access to headers, then all headers are returned. Otherwise, the returned headers will include a 'Content-Type' header at the very least.
However, MockMultipartHttpServletRequest.getMultipartHeaders() in fact returns null if the content type for the part cannot be retrieved. This is because Spring's MultipartRequest.getMultipartContentType() specifies that it will return the associated content type, or null if not defined. Thus, there is definitely a conflict between these two contracts. Most importantly, MockMultipartHttpServletRequest.getMultipartContentType() only supports look-ups for MultipartFile and disregards any Part that might be present.
Deliverables
- Introduce a mock for
javax.servlet.http.Partinspring-testbased on the existingorg.springframework.mock.web.test.MockPartinspring-web, potentially implementingwrite()anddelete()as well.
Reference URL: http://stackoverflow.com/questions/37000457/setting-request-parts-in-spring-test-mvc
Issue Links: - #18826 Support for MockPart in Spring MVC Test ("is depended on by") - #20409 Backport s/s/m/j/o/s/m/w/MockPart.java from 5.x to 4.3.x
Referenced from: commits https://github.com/spring-projects/spring-framework/commit/c8f98ecd8d36b6d270852f0fd5b6b491108c696c
1 votes, 4 watchers
Comment From: spring-projects-issues
Sam Brannen commented
Thanks, Rossen! (y)
Comment From: spring-projects-issues
Sarah Lilie commented
Could someone offer me an example of how to use the MockPart in spring test? I'm trying to mock a request with a MultipartFile and a JSON object
@RequestMapping(method = RequestMethod.POST)
public ResponseEntity<AnalyticMetadata> createObject(@RequestPart("object") CustomObject object, @RequestPart("file") MultipartFile file)
I tried the following request but it complains with exception "Required request part 'object' is not present"
MockMultipartFile multiPartFile = new MockMultipartFile("file", "filename.zip", null, is);
MockPart part = new MockPart("object", mapper.writeValueAsBytes(customObject));
mockMvc.perform(MockMvcRequestBuilders.multipart("/").file(multiPartFile).part(part)).andExpect(status().isCreated())
.andExpect(jsonPath("$.id").isNotEmpty());
Comment From: spring-projects-issues
Sam Brannen commented
Hi Sarah Lilie,
The following is an example taken from the org.springframework.test.web.servlet.samples.standalone.MultipartControllerTests test class in Spring's own test suite.
@Test
public void multipartRequestWithServletParts() throws Exception {
byte[] fileContent = "bar".getBytes(StandardCharsets.UTF_8);
MockPart filePart = new MockPart("file", "orig", new ByteArrayInputStream(fileContent));
byte[] json = "{\"name\":\"yeeeah\"}".getBytes(StandardCharsets.UTF_8);
MockPart jsonPart = new MockPart("json", "json", new ByteArrayInputStream(json));
jsonPart.getHeaders().setContentType(MediaType.APPLICATION_JSON);
standaloneSetup(new MultipartController()).build()
.perform(multipart("/test-multipartfile").part(filePart).part(jsonPart))
.andExpect(status().isFound())
.andExpect(model().attribute("fileContent", fileContent))
.andExpect(model().attribute("jsonContent", Collections.singletonMap("name", "yeeeah")));
}
Hope this helps!
Comment From: spring-projects-issues
Sarah Lilie commented
Hi @Sam Brannen, thanks for the tip! I was able to get it working! :)
Comment From: IgnacioAgustinCabral
This helped me A LOT!! THANKS