I'm trying to test (through Spring test (mvc)) a controller that uses servletRequest.getParts()
All I've read so far is that MockMvcRequestBuilders.fileUpload().file()
is the solution. However I cannot make it work. I wrote the following test which fails
MockMultipartHttpServletRequestBuilder builder = MockMvcRequestBuilders.fileUpload("/foo")
.file(new MockMultipartFile("file", new byte[] { 1, 2, 3, 4 }));
MockHttpServletRequest rq = builder.buildRequest(null);
Assert.assertEquals(1, rq.getParts().size()); // result 0
I went through spring code, and the call to file(...)
adds an element in List<MockMultipartFile>
when getParts()
gets its elements from another list (Map<String, Part> parts)
There must be another way to do it...
Edit 1
The code I'm using to test the controller is :
ResultActions result = mockMvc.perform(
MockMvcRequestBuilders.fileUpload(new URI("/url")).file("param", "expected".getBytes()))
MultipartFile
isn't aPart
so that won't work. You can only add parts and afaik that currently isn't possible with MockMvc. Your only change probably is to mock thePart
and add it yourself by creating a custom RequestBuilder. – Chimera