mockmvc download functionality for contentType "application/octet-stream" throws FileNotFound error
<p>I am new to the mockmvc api.</p>

<p>I am trying to write units for my controllers and one of the method performs download. Please find the code snippet:</p>

<p>Controller :</p>

<pre><code>@RequestMapping(value = "/download-template", method = RequestMethod.GET, produces = MediaType.APPLICATION_OCTET_STREAM_VALUE)
    @ResponseBody
    public FileSystemResource downloadTemplate(HttpServletRequest request,
            HttpServletResponse response) {

        logger.info("User ID: " + request.getAttribute("userId")
                + " - POST: /upload/download-template");

        String rootDir = config.getBaseFolder();
        DownloadUploadTemplateResponse res = uploadService.downloadTemplate(
                rootDir, false);

        response.setHeader("Content-Disposition",
                "attachment; filename=" + res.getFileName());

        return new FileSystemResource(res.getTemplateFile());

    }
</code></pre>

<h2>Unit test code:</h2>

<pre><code>@Test
    public void testDownloadTemplate(){
        DownloadResponse res = new DownloadResponse();
        String templateFile = "upload-template.xlsx";
        String fileName = System.getProperty("java.io.tmpdir") + templateFile;
        res.setFileName(fileName);
        res.setTemplateFile(templateFile);
        when(config.getBaseFolder()).thenReturn(System.getProperty("java.io.tmpdir"));
        when(uploadService.downloadTemplate( System.getProperty("java.io.tmpdir"), false)).thenReturn(res);
        //File file = new File(fileName);
        FileSystemResource resource = new FileSystemResource(res.getTemplateFile());
        try{
            ResultActions action = mockMvc.perform(get("/upload/download-template").contentType(APP_OCTET_STREAM_VALUE_UTF8));
            action.andExpect(header().string("Content-Disposition",
                    "attachment; filename=" + res.getFileName()));
            action.andExpect(status().isOk());


        }catch(Exception e){
            e.printStackTrace();
            fail();
        }
    }
</code></pre>

<p>Below line throws java.io.FileNotFoundException: upload-template.xlsx (The system cannot find the file specified):
ResultActions action = mockMvc.perform(get("/upload/download-template").contentType(APP_OCTET_STREAM_VALUE_UTF8));</p>

<p>Please guide what additional I need to add in the httprequestbuilder to resolve this issue.</p>