title: Spring boot multipart file upload integration test - null file
body: <p>I am unable to write a test that uploads a file to my controller.</p>
  
  <p>Of all the occurrences of this problem I have seen nothing has worked out for me.</p>
  
  <p>I am able to upload and store a file from my webapp, but when I run my test, the file in the controller is always null</p>
  
  <p><strong>Application</strong></p>
  
  <pre><code>import com.mangofactory.swagger.configuration.SpringSwaggerConfig;
  import com.mangofactory.swagger.plugin.EnableSwagger;
  import com.mangofactory.swagger.plugin.SwaggerSpringMvcPlugin;
  import com.wordnik.swagger.model.ApiInfo;
  import org.springframework.beans.factory.annotation.Autowired;
  import org.springframework.boot.SpringApplication;
  import org.springframework.boot.autoconfigure.SpringBootApplication;
  import org.springframework.context.annotation.Bean;
  
  @SpringBootApplication
  @EnableSwagger
  public class Application {
  
  private SpringSwaggerConfig springSwaggerConfig;
  
  @Autowired
  public void setSpringSwaggerConfig(SpringSwaggerConfig springSwaggerConfig) {
      this.springSwaggerConfig = springSwaggerConfig;
  }
  
  @Bean
          // Don't forget the @Bean annotation
  public SwaggerSpringMvcPlugin customImplementation() {
      return new SwaggerSpringMvcPlugin(this.springSwaggerConfig).apiInfo(
              apiInfo()).includePatterns("/api/.*");
  }
  
  private ApiInfo apiInfo() {
      return new ApiInfo("Application", "Upload files", "Play nice", "reece@reececo.com", "Go for your life", "DoesNotExist");
  }
  
  public static void main(String[] args) {
          SpringApplication.run(Application.class, args);
      }
  }
  </code></pre>
  
  <p><strong>Controller</strong></p>
  
  <pre><code>@RequestMapping(method = POST)
  @ApiOperation(value = "Multipart file upload", notes = "Upload a file to an ID")
  @ApiResponses(value = { @ApiResponse(code = 200, message = "") })
  public @ResponseBody String uploadFile(MultipartFile file, @PathVariable String key) {
  
  // Store File
  
  }
  </code></pre>
  
  <p><strong>Test</strong></p>
  
  <pre><code>@RunWith(SpringJUnit4ClassRunner.class)
  @SpringApplicationConfiguration(classes = EasyshareApplication.class)
  @WebAppConfiguration
  @IntegrationTest
  public class FileUploadControllerTest {
  
  @Autowired
  private UploadRepository uploadRepository;
  
  private MockMvc restFileMockMvc;
  
  @Before
  public void setup() throws Exception {
      FileController fileController= new FileController();
      ReflectionTestUtils.setField(fileController, "uploadRepository", uploadRepository);
      this.restFileMockMvc = MockMvcBuilders.standaloneSetup(fileController).build();
  }
  
  @Test
  public void testFileUpload() throws Exception{
      MockMultipartFile mockFile = new MockMultipartFile("data", "DATADATADATDATADATA".getBytes());
  
      Upload upload = uploadRepository.save(new Upload("Description"));
      String key = upload.getKey();
  
      restFileMockMvc
              .perform(fileUpload("/api/uploads/" + key + "/file")
                  .file(mockFile)
                      .param("name", "xyz")
                      .contentType(MediaType.MULTIPART_FORM_DATA))
              .andExpect(status().isCreated())
              .andExpect(jsonPath("$.upload").exists())
              .andExpect(jsonPath("$.ID").exists())
              .andExpect(jsonPath("$.filename").exists())
              .andExpect(jsonPath("$.contentType").exists())
              .andExpect(jsonPath("$.length").exists());
  }
  
  @Test
  public void testMissingFileUpload() throws Exception{
      String key = "shouldntNeedAKey";
  
      restFileMockMvc
              .perform(fileUpload("/api/uploads/" + key + "/file"))
              .andExpect(status().isBadRequest());
  }
  }
  </code></pre>