title: Mock Files with PowerMockito
body: <p>I have the following test code:</p>
  
  <pre><code>@RunWith(PowerMockRunner.class)
  @PrepareForTest({Logger.class, Files.class})
  public class TestClass {
  
    private static final List&lt;String&gt; LIST = new ArrayList&lt;String&gt;() {{
      add("some entry");
    }};
    private static final Path PATH = Paths.get("/tmp/foo");
  
    @Before
    public void setup() {
      PowerMockito.spy(Files.class);
      PowerMockito.doReturn(LIST).when(Files.class, "readAllLines", PATH, Charset.defaultCharset());
    }
  
    @Test
    public void test() {}
  }
  </code></pre>
  
  <p>The code above throws a "NoSuchFileException" on the last line.
  Here is the stacktrace:</p>
  
  <blockquote>
    <p>java.nio.file.NoSuchFileException: /tmp/foo   at
    sun.nio.fs.UnixException.translateToIOException(UnixException.java:86)
      at
    sun.nio.fs.UnixException.rethrowAsIOException(UnixException.java:102)
      at
    sun.nio.fs.UnixException.rethrowAsIOException(UnixException.java:107)
      at
    sun.nio.fs.UnixFileSystemProvider.newByteChannel(UnixFileSystemProvider.java:214)
      at java.nio.file.Files.newByteChannel(Files.java:317)   at
    java.nio.file.Files.newByteChannel(Files.java:363)    at
    java.nio.file.spi.FileSystemProvider.newInputStream(FileSystemProvider.java:380)
      at java.nio.file.Files.newInputStream(Files.java:108)   at
    java.nio.file.Files.newBufferedReader(Files.java:2677)    at
    java.nio.file.Files.readAllLines(Files.java:3033)     at
    sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)   at
    sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
      at
    sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
      at java.lang.reflect.Method.invoke(Method.java:606)     at
    org.powermock.reflect.internal.WhiteboxImpl.performMethodInvocation(WhiteboxImpl.java:2014)
      at
    org.powermock.reflect.internal.WhiteboxImpl.doInvokeMethod(WhiteboxImpl.java:885)
      at
    org.powermock.reflect.internal.WhiteboxImpl.invokeMethod(WhiteboxImpl.java:859)
      at org.powermock.reflect.Whitebox.invokeMethod(Whitebox.java:466)   at
    org.powermock.api.mockito.internal.expectation.PowerMockitoStubberImpl.when(PowerMockitoStubberImpl.java:106)
      at TestClass.setup(TestClass.java:29)   at
    sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)   at
    sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
      at
    sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
      at java.lang.reflect.Method.invoke(Method.java:606)     at
    org.junit.internal.runners.MethodRoadie.runBefores(MethodRoadie.java:129)
      at
    org.junit.internal.runners.MethodRoadie.runBeforesThenTestThenAfters(MethodRoadie.java:93)
      at
    org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$PowerMockJUnit44MethodRunner.executeTest(PowerMockJUnit44RunnerDelegateImpl.java:294)
      at
    org.powermock.modules.junit4.internal.impl.PowerMockJUnit47RunnerDelegateImpl$PowerMockJUnit47MethodRunner.executeTestInSuper(PowerMockJUnit47RunnerDelegateImpl.java:127)
      at
    org.powermock.modules.junit4.internal.impl.PowerMockJUnit47RunnerDelegateImpl$PowerMockJUnit47MethodRunner.executeTest(PowerMockJUnit47RunnerDelegateImpl.java:82)
      at
    org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$PowerMockJUnit44MethodRunner.runBeforesThenTestThenAfters(PowerMockJUnit44RunnerDelegateImpl.java:282)
      at
    org.junit.internal.runners.MethodRoadie.runTest(MethodRoadie.java:84)
      at org.junit.internal.runners.MethodRoadie.run(MethodRoadie.java:49)
      at
    org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.invokeTestMethod(PowerMockJUnit44RunnerDelegateImpl.java:207)
      at
    org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.runMethods(PowerMockJUnit44RunnerDelegateImpl.java:146)
      at
    org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$1.run(PowerMockJUnit44RunnerDelegateImpl.java:120)
      at
    org.junit.internal.runners.ClassRoadie.runUnprotected(ClassRoadie.java:34)
      at
    org.junit.internal.runners.ClassRoadie.runProtected(ClassRoadie.java:44)
      at
    org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.run(PowerMockJUnit44RunnerDelegateImpl.java:118)
      at
    org.powermock.modules.junit4.common.internal.impl.JUnit4TestSuiteChunkerImpl.run(JUnit4TestSuiteChunkerImpl.java:101)
      at
    org.powermock.modules.junit4.common.internal.impl.AbstractCommonPowerMockRunner.run(AbstractCommonPowerMockRunner.java:53)
      at
    org.powermock.modules.junit4.PowerMockRunner.run(PowerMockRunner.java:53)
      at
    org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
      at
    org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
      at
    org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
      at
    org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
      at
    org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
      at
    org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)</p>
  </blockquote>
  
  <p>I can't quite get what I am doing wrong here.</p>