Mock files with PowerMockito

I have the following test code:

@RunWith(PowerMockRunner.class)
@PrepareForTest({Logger.class, Files.class})
public class TestClass {

  private static final List<String> LIST = new ArrayList<String>() {{
    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() {}
}

      

The above code throws a "NoSuchFileException" on the last line. Here is the stacktrace:

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) in sun.nio.fs.UnixFileSystemProvider.newByteChannel (UnixFileSystemProvider.java:214) in java.nio.file.Files.newByte.Channel: (Files. .nio.file.Files.newByteChannel (Files.java:363) in java.nio.file.spi.FileSystemProvider.newInputStream (FileSystemProvider.java:380) in 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.performation .java: 2014) at org.powermock.reflect.internal.WhiteboxImpl.doInvokeMethod (WhiteboxImpl.java:885) at org.powermock.reflect.internal.WhiteboxImpl.invokeMethod (WhiteboxImpl.java:859) at org.power. 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.etreflect.pl. .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.in .runners.MethodRoadie.runBeforesThenTestThenAfters (MethodRoadie.java:93) at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl $ PowerMockJUnit44MethodestRunner.exe .PowerMockJUnit47RunnerDelegateImpl $ PowerMockJUnit47MethodRunner.executeTestInSuper (PowerMockJUnit47RunnerDelegateImpl.java:127) in org.powermock.modules.junit4.internal.impl.PowerMockJUnit47RunnerDelegateImpl $ PowerMockJUnit47MethodRunner.executeTest (PowerMockJUnit47RunnerDelegateImpl.java:82) in org.powermock.modules.junit4.internal.impl ...PowerMockJUnit44RunnerDelegateImpl $ PowerMockJUnit44MethodRunner.runBeforesThenTestThenAfters (PowerMockJUnit44RunnerDelegateImpl.java:282) in org.junit.internal.runners.MethodRoadie.runTest (MethodRoadie.java:84) on org.junit.internal.runners.MethodRoadie.run (MethodRoadie.java:49) in org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.invokeTestMethod (PowerMockJUnit44RunnerDelegateImpl.java:207) in org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.runMethods (PowerMockJUnit44RunnerDelegateImpl.java:146) in org.powermock .modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl $ 1.run (PowerMockJUnit44RunnerDelegateImpl. runProtected (ClassRoadie.java:44) at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.run (PowerMockJUnit44RunnerDelegateImpl.java:118) at org.powermock.modules.junit4.common.internal.impl.PowerMock:RunnerDelegateImpl.run at org.powermock.modules.junit4.common.internal.impl.AbstractCommonPowerMockRunner.run (AbstractCommonPowerMockRunner.java:53) at org.powermock.modules.junit4.PowerMockRunner.run (PowerMockRunner.java:53) at .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.java .runner.RemoteTestRunner.runTests (RemoteTestRunner.java:467) in org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests (RemoteTestRunner.java:683) in org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run (RemoteTestRunner.Java: 390) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main (RemoteTestRunner.java:197)

I cannot get what I am doing wrong here.

+3


source to share


1 answer


I found the problem: The problem is Files is a system class.



https://github.com/powermock/powermock/wiki/mocksystem

+3


source







All Articles