title: Setting request parts in spring test mvc
body: <p>I'm trying to test (through Spring test (mvc)) a controller that uses <code>servletRequest.getParts()</code> </p>
  
  <p>All I've read so far is that <code>MockMvcRequestBuilders.fileUpload().file()</code> is the solution. However I cannot make it work. I wrote the following test which fails</p>
  
  <pre><code>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
  </code></pre>
  
  <p>I went through spring code, and the call to <code>file(...)</code> adds an element in <code>List&lt;MockMultipartFile&gt;</code> when <code>getParts()</code> gets its elements from another list <code>(Map&lt;String, Part&gt; parts)</code></p>
  
  <p>There must be another way to do it...</p>
  
  <p><strong>Edit 1</strong></p>
  
  <p>The code I'm using to test the controller is :</p>
  
  <pre><code>ResultActions result = mockMvc.perform(
              MockMvcRequestBuilders.fileUpload(new URI("/url")).file("param", "expected".getBytes()))
  </code></pre>