'How to PUT multipart/form-data using Spring MockMvc?
I have a controller's method with a PUT method, which receives multipart/form-data:
@RequestMapping(value = "/putIn", method = RequestMethod.PUT)
public Foo updateFoo(HttpServletRequest request,
@RequestBody Foo foo,
@RequestParam("foo_icon") MultipartFile file) {
...
}
and I want to test it using MockMvc. Unfortunately MockMvcRequestBuilders.fileUpload creates essentially an instance of MockMultipartHttpServletRequestBuilder which has a POST method:
super(HttpMethod.POST, urlTemplate, urlVariables)
EDIT:
Surely I can I can not create my own implementation of MockHttpServletRequestBuilder, say
public MockPutMultipartHttpServletRequestBuilder(String urlTemplate, Object... urlVariables) {
super(HttpMethod.PUT, urlTemplate, urlVariables);
super.contentType(MediaType.MULTIPART_FORM_DATA);
}
because MockHttpServletRequestBuilder has a package-local constructor.
But I'm wondering is there any more convenient Is any way to do this, may be I missed some existent class or method for doing this?
Solution 1:[1]
This is unfortunately currently not supported in Spring MVC Test, and I don't see a work-around other than creating your own custom MockPutMultipartHttpServletRequestBuilder and copying-n-pasting code from the standard implementation.
For what it's worth, Spring MVC also does not support PUT requests for file uploads by default either. The Multipart resolvers are hard coded to accept only POST requests for file uploads -- both for Apache Commons and the standard Servlet API support.
If you would like Spring to support PUT requests in addition, feel free to open a ticket in Spring's JIRA issue tracker.
Solution 2:[2]
Translating @HammerNl answer for Kotlin. This worked for me.
val file = File("/path/to/file").readBytes()
val multipartFile = MockMultipartFile("image", "image.jpg", "image/jpg", file)
val postProcess = RequestPostProcessor { it.method = "PUT"; it}
mockMvc.perform(
MockMvcRequestBuilders.multipart("/api/image/$id")
.file(multipartFile)
.with(postProcess))
.andExpect(MockMvcResultMatchers.status().isOk)
Solution 3:[3]
You can pass both foo and file
Try rewrite you controller like:
@RequestMapping(value = "/putIn", method = RequestMethod.PUT)
public Foo updateFoo(
HttpServletRequest request,
@RequestPart Foo foo,
@RequestPart MultipartFile file) {
...
}
And test looks like:
MockMultipartFile file = new MockMultipartFile("file", "dummy.csv",
"text/plain", "Some dataset...".getBytes());
// application/json if you pass json as string
MockMultipartFile file2 = new MockMultipartFile("foo", "foo.txt",
"application/json", "Foo data".getBytes());
MockMultipartHttpServletRequestBuilder builder =
MockMvcRequestBuilders.multipart("/test1/datasets/set1");
builder.with(new RequestPostProcessor() {
@Override
public MockHttpServletRequest postProcessRequest(MockHttpServletRequest request) {
request.setMethod("PUT");
return request;
}
});
mvc.perform(builder
.file(file)
.file(file2))
.andExpect(status().ok());
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Sam Brannen |
| Solution 2 | orpheus |
| Solution 3 |
