Test file upload Spring MVC
<p>I would like to test file uploading by REST API. I found it quite confusing to send file as <code>RequestParam</code> instead of <code>RequestBody</code>.</p>

<p>Controller method:</p>

<pre><code>    @PostMapping(value = "/upload", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
public
@ResponseBody
ResponseEntity&lt;String&gt; uploadFile(
        @RequestParam(name = "file") MultipartFile multipartFile,
        @RequestParam(name = "path") String path) {
    logger.debug("File upload REST requested");

    return new ResponseEntity&lt;&gt;(fileService.uploadFile(
            multipartFile, path),
            HttpStatus.OK);
}
</code></pre>

<p>1.Now I would like to test it and I've used mocks. While debugging it, I see that mock service working but method exactly with this arguments is not invoked, so the test if failed. How could I handle this?</p>

<pre><code>@Test
public void testUploadFile() throws Exception {
    String mockName = "mock";
    MockMultipartFile mockMultipartFile = new MockMultipartFile(mockName, mockName.getBytes());

    when(mockFileService.uploadFile(mockMultipartFile, rootDir)).thenReturn("success");

    mockMvc.perform(MockMvcRequestBuilders.fileUpload("/files/upload")
            .file("file", mockMultipartFile.getBytes())
            .param("path", rootDir))
            .andExpect(status().isOk())
            .andExpect(content().string("success"));

    verify(mockFileService, times(1)).updateFile(mockMultipartFile, rootDir);
    verifyNoMoreInteractions(mockFileService);
}
</code></pre>

<p>2.How could I test this with Postman? I see that I can send file in Body, but how could I send it as param?</p>

<p><strong>EDIT:</strong></p>

<p>I've changed the method, but the problem is not there:</p>

<pre><code>Argument(s) are different! Wanted:
mockFileService.uploadFile(
org.springframework.mock.web.MockMultipartFile@61bd0845,
"/"
);
</code></pre>

<p>Looks like method are using 2 instances of MockMultipartFile. And the second question is still open, how could this method be tested from Postman?</p>