java - 如何使用 Mockito 模拟 Java Path API?

标签 java unit-testing path mocking mockito

Java Path API 是 Java File API 的更好替代品,但静态方法的大量使用使得 Mockito 难以模拟。 在我自己的类(class)中,我注入(inject)了一个 FileSystem 实例,我在单元测试期间将其替换为模拟。

但是,我需要模拟很多方法(并且还创建了很多模拟)来实现这一点。这种情况在我的测试类(class)中反复发生了很多次。因此,我开始考虑设置一个简单的 API 来注册 Path-s 并声明相关行为。

例如,我需要检查流打开时的错误处理。 主类:

class MyClass {
    private FileSystem fileSystem;

    public MyClass(FileSystem fileSystem) {
        this.fileSystem = fileSystem;
    }

    public void operation() {
        String filename = /* such way to retrieve filename, ie database access */
        try (InputStream in = Files.newInputStream(fileSystem.getPath(filename))) {
            /* file content handling */
        } catch (IOException e) {
            /* business error management */
        }
    }
}

测试类:

 class MyClassTest {

     @Test
     public void operation_encounterIOException() {
         //Arrange
         MyClass instance = new MyClass(fileSystem);

         FileSystem fileSystem = mock(FileSystem.class);
         FileSystemProvider fileSystemProvider = mock(FileSystemProvider.class);
         Path path = mock(Path.class);
         doReturn(path).when(fileSystem).getPath("/dir/file.txt");
         doReturn(fileSystemProvider).when(path).provider();
         doThrow(new IOException("fileOperation_checkError")).when(fileSystemProvider).newInputStream(path, (OpenOption)anyVararg());

         //Act
         instance.operation();

         //Assert
         /* ... */
     }

     @Test
     public void operation_normalBehaviour() {
         //Arrange
         MyClass instance = new MyClass(fileSystem);

         FileSystem fileSystem = mock(FileSystem.class);
         FileSystemProvider fileSystemProvider = mock(FileSystemProvider.class);
         Path path = mock(Path.class);
         doReturn(path).when(fileSystem).getPath("/dir/file.txt");
         doReturn(fileSystemProvider).when(path).provider();
         ByteArrayInputStream in = new ByteArrayInputStream(/* arranged content */);
         doReturn(in).when(fileSystemProvider).newInputStream(path, (OpenOption)anyVararg());

         //Act
         instance.operation();

         //Assert
         /* ... */
     }
 }

我有很多此类类/测试,模拟设置可能更棘手,因为静态方法可能会通过 Path API 调用 3-6 个非静态方法。我重构了测试以避免大多数冗余代码,但随着 Path API 使用量的增长,我的简单 API 往往非常有限。所以又到了重构的时候了。

但是,我正在考虑的逻辑似乎很难看,并且需要大量代码才能实现基本用法。我想简化 API 模拟(无论是否是 Java Path API)的方法基于以下原则:

  1. 创建实现接口(interface)或扩展类以模拟的抽象类。
  2. 实现我不想模拟的方法。
  3. 当调用“部分模拟”时,我想执行(按优先顺序):明确模拟的方法、实现的方法、默认答案。

为了实现第三步,我考虑创建一个 Answer 来查找已实现的方法并回退到默认答案。然后在模拟创建时传递此 Answer 的实例。

是否有直接从 Mockito 或其他方法来处理问题的现有方法?

最佳答案

你的问题是你违反了Single Responsibility Principle .

您有两个顾虑:

  1. 查找并定位一个文件,获取一个InputStream
  2. 处理文件。
    • 实际上,这很可能也应该分解为子关注点,但这超出了这个问题的范围。

您正试图以一种方式完成这两项工作,这迫使您做大量的额外工作。相反,将工作分成两个不同的类别。例如,如果您的代码是这样构造的:

class MyClass {
  private FileSystem fileSystem;
  private final StreamProcessor processor;

  public MyClass(FileSystem fileSystem, StreamProcessor processor) {
    this.fileSystem = fileSystem;
    this.processor = processor;
  }

  public void operation() {
    String filename = /* such way to retrieve filename, ie database access */
    try (InputStream in = Files.newInputStream(fileSystem.getPath(filename))) {
        processor.process(in);
    } catch (IOException e) {
        /* business error management */
    }
  }
}
class StreamProcessor {
  public StreamProcessor() {
    // maybe set dependencies, depending on the need of your app
  }

  public void process(InputStream in) throws IOException {
    /* file content handling */
  }
}

现在我们将责任分为两个地方。从 InputStream 中执行您要测试的所有业务逻辑工作的类只需要一个输入流。事实上,我什至不会 mock 它,因为它只是数据。您可以以任何方式加载 InputStream ,例如使用您在问题中提到的 ByteArrayInputStream在您的StreamProcessor 测试中不需要任何 Java Path API 代码。

此外,如果您以通用方式访问文件,则只需进行一次测试即可确保该行为有效。您还可以将 StreamProcessor 设为一个接口(interface),然后在您的代码库的不同部分,针对不同类型的文件执行不同的工作,同时传入不同的 StreamProcessor s 进入文件 API。


在评论中你说:

Sounds good but I have to live with tons of legacy code. I'm starting to introduce unit test and don't want to refactor too much "application" code.

最好的方法就是我上面说的。但是,如果您想进行最小的更改以添加测试,那么您应该这样做:

旧代码:

public void operation() {
  String filename = /* such way to retrieve filename, ie database access */
  try (InputStream in = Files.newInputStream(fileSystem.getPath(filename))) {
    /* file content handling */
  } catch (IOException e) {
    /* business error management */
  }
}

新代码:

public void operation() {
  String filename = /* such way to retrieve filename, ie database access */
  try (InputStream in = Files.newInputStream(fileSystem.getPath(filename))) {
    new StreamProcessor().process(in);
  } catch (IOException e) {
    /* business error management */
  }
}
public class StreamProcessor {
  public void process(InputStream in) throws IOException {
    /* file content handling */
    /* just cut-paste the other code */
  }
}

这是执行我上面描述的最少侵入性的方法。我描述的原始方式更好,但显然这是一个更复杂的重构。这种方式应该几乎不涉及其他代码更改,但允许您编写测试。

关于java - 如何使用 Mockito 模拟 Java Path API?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32271703/

相关文章:

asp.net - 网络测试软件

javascript - Angular 中对 requestAnimationFrame 进行单元测试的选项有哪些?

javascript - A* 在 Canvas 上寻路

java - Dijkstra - 定位上一个 vector

java - 如何在java的静态方法中使用this关键字?

java - 是否可以让 Nashorn 从类路径加载脚本?

java - 从java中的数组中找出n个缺失元素

ruby - 使用 RSpec 上传图像在 Controller 上有意外的类

Linux 找不到 metis.h

java - PDFBox如何提取超链接信息