Can't upload multipart file with mockMvc
<p>I can't figure out how to upload jpg file using mockMvc with spock. When i perform request manually  with postman everything works fine and image uploads. I am using spring boot</p>

<p>Here is the controller : </p>

<pre><code>@PostMapping(value = "/items/{id}/images")
public ResponseEntity&lt;?&gt; uploadImage(@PathVariable Long id, @RequestParam("file") MultipartFile file,
                                     UriComponentsBuilder ucb) {

    if (file.isEmpty()) throw new IllegalArgumentException("File is empty");

    Optional&lt;Image&gt; image = itemService.addImageToItem(id, file);

    if (!image.isPresent()) {
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("Unable to upload image");
    }

    URI location = ucb.path("/items/")
            .path(String.valueOf(id))
            .path("/images/")
            .path(image.get().getId().toString()).build().toUri();

    return ResponseEntity.created(location).contentType(MediaType.APPLICATION_JSON).body(image.get());

}
</code></pre>

<p>and here is the test:</p>

<pre><code>@SpringBootTest
@AutoConfigureMockMvc
class FileUploadTest extends Specification {


    @Autowired
    MockMvc mockMvc;

    @Shared
    MockMultipartFile multiPartImage;

    @Shared byte[] content = null;


    def setupSpec() {

        Path path = new ClassPathResource("/imagesToUpload/apokalipsa.jpeg").getFile().toPath();
        String name = "apokalipsa.jpeg";
        String originalFileName = "apokalipsa.jpeg";
        String contentType = MediaType.IMAGE_JPEG_VALUE;

        try {
            content = Files.readAllBytes(path);
        } catch (final IOException e) {
            e.printStackTrace()
        }

        multiPartImage = new MockMultipartFile(name,
                originalFileName, contentType, content);
    }


    @Sql("/testSql/personItem.sql")
    def "should upload image"() {

        expect:
        mockMvc.perform(fileUpload("/items/2/images").file(multiPartImage))
                .andExpect(status().isCreated())
     }
    }
</code></pre>

<p>i think that everything is fine with multipart file because I have tested it...</p>

<p>and here is the request and the response:</p>

<pre><code>MockHttpServletRequest:
  HTTP Method = POST
  Request URI = /items/2/images
   Parameters = {}
      Headers = {Content-Type=[multipart/form-data;charset=UTF-8]}

Handler:
         Type = controller.ItemController
       Method = public org.springframework.http.ResponseEntity&lt;?&gt; controller.ItemController.uploadImage(java.lang.Long,org.springframework.web.multipart.MultipartFile,org.springframework.web.util.UriComponentsBuilder)

Async:
Async started = false
 Async result = null

Resolved Exception:
         Type = org.springframework.web.multipart.support.MissingServletRequestPartException

ModelAndView:
    View name = null
         View = null
        Model = null

FlashMap:
   Attributes = null

MockHttpServletResponse:
       Status = 400
Error message = Required request part 'file' is not present
      Headers = {X-Application-Context=[application:-1]}
 Content type = null
         Body = 
Forwarded URL = null
   Redirected URL = null
      Cookies = []
</code></pre>

<p>I would really appreciate help, I have tried many variations of request but without avail</p>