【问题标题】:How to mock Inputstream to load Properties in Java如何模拟 Inputstream 以在 Java 中加载属性
【发布时间】:2016-04-28 01:24:19
【问题描述】:

我想测试properties.load(in) 方法抛出的IOException 和IllegalArgumentException。根据 OracleDoc 此处的文档,它说加载方法会抛出 IOException - 如果从输入流读取时发生错误。 IllegalArgumentException - 如果输入流包含格式错误的 Unicode 转义序列。

这是我的代码:

public class PropertiesRetriever {

private String foo;
private String foo1;
private Properties properties;

/**
 * Injects the properties file Path in the {GuiceModule}
 * Calls {@link PropertiesRetriever#loadPropertiesPath(String) to load the
 * properties file.
 */

@Inject
public PropertiesRetriever(@Named("propertiesPath") String propertiesPath,     Properties properties)
   throws IOException {
this.properties = properties;
loadPropertiesPath(propertiesPath);
}

/**
 * Loads the properties file as inputstream.
 * 
 */
public void loadPropertiesPath(String path) throws IOException {
InputStream in = this.getClass().getResourceAsStream(path);
properties.load(in);

}

这里是一个方法:

 properties.load(in)

抛出 IOException 和 IllegalArgumentException。我想在 JUnit 测试中测试这种方法。无论如何我可以调用这些方法。

【问题讨论】:

标签: java android inputstream junit4


【解决方案1】:

您可以通过稍微重构代码来做到这一点。那并使用 Mockito 或其他一些模拟框架来创建一个行为如你所愿(抛出异常)的 InputStream:

public void loadPropertiesPath(String path) throws IOException {
    // Always close streams you open!
    try (InputStream in = getIStream(path)) {
        properties.load(in);
    }
}

private InputStream getIStream(String path) {
   InputStream in = this.getClass().getResourceAsStream(path);

   return in;
}

你可以使用 mockito 来create a partial mock 你的对象;模拟 getIStream(String) 返回模拟 InputStream。设置模拟以在调用 InputStream::read(byte[]) 时抛出您想要的异常。

如果您不想使用 PowerMock,则可以将 getIStream(String) 的可见性更改为 default。然后普通的 mockito 就可以完成这项工作:

@Test
public void exceptionTest() throws IOException {
    PropertiesRetriever pr = new PropertiesRetriever();
    PropertiesRetriever prSpy = spy(pr);

    InputStream isMock = mock(InputStream.class);
    doReturn(isMock).when(prSpy).getIStream(anyString());

    doThrow(new IllegalArgumentException("CRASH!")).when(isMock).read(any());

    prSpy.loadPropertiesPath("blah");
}

【讨论】:

  • 我实际上是想避免使用 PowerMock 或 Matchers.any() 我打算只使用 Mockito。
  • 它仍然没有抛出 IOException 或 IllegalArgumentException。无论如何我可以创建一个损坏的输入流吗?还是我在这里遗漏了什么?
  • @Jasmine,你可以在doThrow()中指定需要的异常。我在示例中使用了IllegalArgumentException,但您也可以抛出IOException 及其子类或任何RuntimeException。
  • 我可以模拟我正在测试的课程吗?您指定的方法模拟要测试的类。这有效吗?
  • @Jasmine 是的。在 Mockito 术语中,它被称为部分模拟或“间谍”。
【解决方案2】:

你有两个选择。要么提供一些测试文件,这将产生预期的错误,要么将 Stream 的模拟作为参数传递给 Properties 检索器。因此,您将直接使用 inputStream 而不是 propertiesPath 参数(这种方法可能只是将您的问题转移到其他地方)。

如果你决定将 Stream 作为参数传递,有一些提示,如何模拟它:Mocking Java InputStream

【讨论】:

  • 我通过了 InputStream 的模拟,但它仍然没有抛出 IOException。有什么方法可以测试吗?
  • 如果我使用 ArgumentCaptor 捕获 Inputstream.class 并使用 doThrow(IOException.class).when(mockProperties).load(reqCaptor.capture()); 会是个好主意吗?最后调用 githubpropertiesRetreiver.loadPropertiesPath(filePath);并检查 IOException?
猜你喜欢
  • 1970-01-01
  • 2011-09-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-03-25
  • 1970-01-01
  • 2012-01-02
  • 1970-01-01
相关资源
最近更新 更多