dev*_*v ツ 5 java junit properties ioexception
我正在从 Java 项目的类路径中读取文件。
示例代码:
public static Properties loadPropertyFile(String fileName) {
Properties properties = new Properties();
InputStream inputStream = PropertyReader.class.getClassLoader().getResourceAsStream(fileName);
if (inputStream != null) {
try {
properties.load(inputStream);
} catch (IOException e) {
e.printStackTrace();
}
} else {
throw new Exception("Property file: [" + fileName + "] not found in the classpath");
}
return properties;
}
Run Code Online (Sandbox Code Playgroud)
运行良好。我正在为此代码编写 Junit 测试。如何为IOExceptionin创建场景properties.load(inputStream)?
我应该在properties.file中放入什么值才能获得IOException?
您考虑过模拟框架吗?模拟newProperties 的运算符以返回一个模拟实现,该实现在调用IOException其方法时会抛出异常。load
mockitio 和 powermockito 可以让你做到这一点。