如何使用powermock with Mockito从files.java对newDirectoryStream进行部分



我想部分模拟文件。

当我使用模拟静态时,下面的示例非常好。不幸的是,这种方法模拟了所有静态方法。我想仅嘲笑一个特定的特定,因此想通过调用间谍方法使用部分模拟。

import java.io.IOException;
import java.nio.file.DirectoryStream;
import java.nio.file.Files;
import java.nio.file.Path;
public class FileAccess {
    public static void newDirectoryStreamWrapper(Path path) throws IOException {
        DirectoryStream<Path> paths = Files.newDirectoryStream(path);
        if (paths == null) {
            return;
        }
        for (Path p : paths) {
                // some work
        }
    }
}
import org.junit.Assert;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import org.junit.runner.RunWith;
import org.mockito.Mockito;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import java.io.File;
import java.io.IOException;
import java.nio.file.DirectoryStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Map;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
@RunWith(PowerMockRunner.class)
@PrepareForTest({Files.class, FileAccess.class})
public class Mocking {
    private boolean called = false;
    @Rule
    public TemporaryFolder folder = new TemporaryFolder();
    @Test
    public void test() throws Exception {
        //test will pass with mockStatic instead of spy
        PowerMockito.spy(Files.class);
        Mockito
            .when(Files.newDirectoryStream(Mockito.any(Path.class)))
            .thenAnswer(new Answer<Object>() {
                @Override
                public Object answer(InvocationOnMock invocationOnMock) throws Throwable {
                    called = true;
                    return invocationOnMock.callRealMethod();
                }
            });
        Path path = folder.newFile().toPath();
        FileAccess.newDirectoryStreamWrapper(path.getParent());
        assertTrue(called);
    }
}

在上述执行代码时引发了例外:

java.lang.NullPointerException
    at java.nio.file.Files.provider(Files.java:97)
    at java.nio.file.Files.newDirectoryStream(Files.java:457)
    at Mocking.test(Mocking.java:41)
    at org.junit.internal.runners.TestMethod.invoke(TestMethod.java:68)
    at org.junit.internal.runners.MethodRoadie$2.run(MethodRoadie.java:89)
    at org.junit.internal.runners.MethodRoadie.runBeforesThenTestThenAfters(MethodRoadie.java:97)
    at org.junit.rules.ExternalResource$1.evaluate(ExternalResource.java:48)
    at org.junit.internal.runners.MethodRoadie.runTest(MethodRoadie.java:87)
    at org.junit.internal.runners.MethodRoadie.run(MethodRoadie.java:50)
    at org.junit.internal.runners.ClassRoadie.runUnprotected(ClassRoadie.java:34)
    at org.junit.internal.runners.ClassRoadie.runProtected(ClassRoadie.java:44)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
    at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
    at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
    at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)

mockStatic并不模拟所有方法,它仅对所有方法进行模拟。因此,只要您实际上没有模拟方法,即使在mockStatic()之后,实际方法仍然可以调用。仅在模拟静态之后明确嘲笑的方法才会以模拟行为做出响应。

因此您使用mockStatic的方法是正确的。

相关内容

  • 没有找到相关文章

最新更新