title: Junit - mock a file
body: <p>I'm trying to cover code that process a file. I'm trying to avoid using real file for tests, so I'm using Mockito.
  This is the code I'm trying to test:</p>
  
  <pre><code>try {
      byte[] data = Files.readAllBytes(((File) body).toPath());
      immutableBody = data;
      actualHeaderParams.put(HttpHeaders.CONTENT_LENGTH, (new Integer(data.length)).toString());
      contentType = MediaType.APPLICATION_OCTET_STREAM;
    }
  </code></pre>
  
  <p>I'm using mock file:</p>
  
  <pre><code>File mockedFile = Mockito.mock(File.class);
  </code></pre>
  
  <p>but I get an Exception on 'toPath'. So I added some path or null, but then again I get Exceptions since the file doesn't exist in the path.</p>
  
  <pre><code>when(mockedFile.toPath()).thenReturn(Paths.get("test.txt"));
  </code></pre>
  
  <p>getting:</p>
  
  <pre><code>com.http.ApiException: There was a problem reading the file: test.txt
  </code></pre>
  
  <p>Is there any way doing it without creating a real file for the test?</p>