PowerMockito的模拟文件



我有以下测试代码:

@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() {}
}

上面的代码在最后一行抛出一个"NoSuchFileException"。这是堆叠竞赛:

java.nio.file.NoSuchFileException:/tmp/foosun.nio.fs.UnixException.translateToIOException(UnixExcession.java:86)在sun.nio.fs.UnixException.rethrowAsIOException(UnixException.java:102)在sun.nio.fs.UnixException.rethrowAsIOException(UnixException.java:107)在sun.nio.fs.UnixFileSystemProvider.newByteChannel(UnixFileSystem Provider.java:214)位于java.nio.file.Files.newByteChannel(Files.java:317)java.nio.file.Files.newByteChannel(Files.java:363)位于java.nio.file.spi.FileSystemProvider.newInputStream(FileSystemProvider.java:380)位于java.nio.file.Files.newInputStream(Files.java:108)java.nio.file.Files.newBufferedReader(Files.java:2677)位于java.nio.file.Files.readAllLines(Files.java:3033)位于位于的sun.reflect.NativeMethodAccessorImpl.invoke0(本机方法)sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)在sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)在java.lang.reflect.Method.ioke(Method.java:606)org.powermock.reflect.internal.WhiteboxImpl.performMethodInvocation(WhiteboxImpl.java:2014)在org.powermock.reflect.internal.WhiteboxImpl.doInvokeMethod(WhiteboxImpl.java:885)在org.powermock.reflect.internal.WhiteboxImpl.invokeMethod(WhiteboxImpl.java:859)网址:org.powermock.reflect.Whitebox.invokeMethod(Whitebox.java:466)org.powermock.api.mokito.internal.expection.PowerMockitoStubberImpl.when(PowerMockitoStubberImpl.java:106)在TestClass.setup(TestClass.java:29)位于的sun.reflect.NativeMethodAccessorImpl.invoke0(本机方法)sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)在sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)在java.lang.reflect.Method.ioke(Method.java:606)org.junit.internal.runners.MethodRoadie.runBefores(MethodRoadie.java:129)在org.junit.internal.runners.MethodRoadie.runBeforestThenAfters(MethodRoadie.java:93)在org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$PowerMockJUnit44MethodRunner.executeTest在org.powermock.modules.junit4.internal.impl.PowerMockJUnit47RunnerDelegateImpl$PowerMockJUnit47MethodRunner.executeTestInSuper(PowerMockjunit47Runner Delegateimpl.java:127)在org.powermock.modules.junit4.internal.impl.PowerMockJUnit47RunnerDelegateImpl$PowerMockJUnit47MethodRunner.executeTest(PowerMockJunit47Runner Delegateimpl.java:82)在org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$PowerMockJUnit44MethodRunner.runBeforesThenTestThenAfters(PowerMockjunit44Runner Delegateimpl.java:282)在org.junit.internal.runners.MethodRoadie.runTest(MethodRoadie.java:84)网址:org.junit.internal.runners.MethodRoadie.run(MethodRoadie.java:49)在org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.invokeTestMethod在org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.runMethods(PowerMockjunit44Runner Delegateimpl.java:146)在org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$1.run(PowerMockjunit44Runner Delegateimpl.java:120)在org.junit.internal.runners.ClassRoadie.runUnprotected(ClassRoadie.java:34)在org.junit.internal.runners.ClassRoadie.runProtected(ClassRoadie.java:44)在org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.run(PowerMockjunit44Runner Delegateimpl.java:118)在org.powermock.modules.junit4.common.internal.impl.JUnit4TestSuiteChunkerImpl.run(JUnit4Test SuiteChuckerImpl.java:101)在org.powermock.modules.junit4.common.internal.impl.AbstractCommonPowerMockRunner.run(AbstractCommonPower-MockRunner.java:53)在org.powermock.modules.junit4.PowerMockRunner.run(PowerMockRuner.java:53)在org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TtestReference.java:50)在org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)在org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)在org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)在org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)在org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)

我不太明白我在这里做错了什么。

我发现了问题:问题是Files是System类。

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

相关内容

  • 没有找到相关文章

最新更新