Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Mockito使用Mockito.mockStatic()模拟静态void方法_Java_Spring_Spring Boot_Unit Testing_Mockito - Fatal编程技术网

Java Mockito使用Mockito.mockStatic()模拟静态void方法

Java Mockito使用Mockito.mockStatic()模拟静态void方法,java,spring,spring-boot,unit-testing,mockito,Java,Spring,Spring Boot,Unit Testing,Mockito,我正在使用Spring Boot,在我的一个单元测试中,我需要模拟Files.delete(somePath)函数。这是一种静态空洞方法 我知道使用Mockito可以模拟void方法: doNothing().when(MyClass.class).myVoidMethod() 自2020年7月10日起,可以模拟静态方法: try (MockedStatic<MyStaticClass> mockedStaticClass = Mockito.mockStatic(MyStatic

我正在使用Spring Boot,在我的一个单元测试中,我需要模拟
Files.delete(somePath)
函数。这是一种静态空洞方法

我知道使用Mockito可以模拟void方法:

doNothing().when(MyClass.class).myVoidMethod()
自2020年7月10日起,可以模拟静态方法:

try (MockedStatic<MyStaticClass> mockedStaticClass = Mockito.mockStatic(MyStaticClass.class)) {
    mockedStaticClass.when(MyStaticClass::giveMeANumber).thenReturn(1L);
    assertThat(MyStaticClass.giveMeANumber()).isEqualTo(1L);
  }
有没有不使用PowerMockito模拟静态void方法的方法?

如果可能的话,正确的语法是什么?

一般来说,模拟静态调用是最后的手段,不应该作为默认方法使用

例如,对于使用文件系统测试代码,有更好的方法。例如,根据junit的
版本,使用或

另外,请注意,
Mockito.mockStatic
可能会显著降低您的测试速度(例如,查看下面的注释)

说到上面的警告,找到下面的代码片段,它显示了如何测试,该文件被删除了

class FileRemover {
    public static void deleteFile(Path filePath) throws IOException {
        Files.delete(filePath);
    }
}

class FileRemoverTest {

    @TempDir
    Path directory;

    @Test
    void fileIsRemovedWithTemporaryDirectory() throws IOException {
        Path fileToDelete = directory.resolve("fileToDelete");
        Files.createFile(fileToDelete);

        FileRemover.deleteFile(fileToDelete);

        assertFalse(Files.exists(fileToDelete));
    }

    @Test
    void fileIsRemovedWithMockStatic() throws IOException {
        Path fileToDelete = Paths.get("fileToDelete");
        try (MockedStatic<Files> removerMock = Mockito.mockStatic(Files.class)) {
            removerMock.when(() -> Files.delete(fileToDelete)).thenAnswer((Answer<Void>) invocation -> null);
            // alternatively
            // removerMock.when(() -> Files.delete(fileToDelete)).thenAnswer(Answers.RETURNS_DEFAULTS);

            FileRemover.deleteFile(fileToDelete);

            removerMock.verify(() -> Files.delete(fileToDelete));
        }
    }
}
类文件删除程序{
公共静态void deleteFile(路径filePath)引发IOException{
文件。删除(文件路径);
}
}
类文件删除测试{
@坦普迪尔
路径目录;
@试验
void fileIsRemovedWithTemporaryDirectory()引发IOException{
路径fileToDelete=directory.resolve(“fileToDelete”);
Files.createFile(fileToDelete);
deleteFile(fileToDelete);
assertFalse(Files.exists(fileToDelete));
}
@试验
void fileIsRemovedWithMockStatic()引发IOException{
Path fileToDelete=Path.get(“fileToDelete”);
try(MockedStatic removemock=Mockito.mockStatic(Files.class)){
RemoveMock.when(()->Files.delete(fileToDelete)).thenAnswer((Answer)调用->null);
//或者
//RemoveMock.when(()->Files.delete(fileToDelete)).thenAnswer(Answers.RETURNS_默认值);
deleteFile(fileToDelete);
removerMock.verify(()->Files.delete(fileToDelete));
}
}
}
注意事项:

  • Mockito.mockStatic
    在mockito3.4及更高版本中可用,请检查您使用的版本是否正确

  • 该代码段详细地展示了两种方法:
    @TempDir
    Mockito.mockStatic
    。当运行这两个测试时,您会注意到
    Mockito.mockStatic
    要慢得多。例如,在我使用
    Mockito进行的系统测试中,mockStatic大约运行900毫秒,而
    @TempDir
    运行10毫秒


  • 您是否将mockito内联添加为依赖项?是的,我将编辑我的问题以添加POM.xml文件。非常感谢您的回答!我知道模仿静态方法不是一个好的实践,但我不知道它有那么慢。谢谢你解释正确的方法,我会实施它。