我有以下代码片段:
try {
Properties properties = new Properties();
properties.load(Thread.currentThread().getContextClassLoader()
.getResourceAsStream("application.properties"));
String accessKey = properties.getProperty("");
String secretKey = properties.getProperty("");
return new BasicAWSCredentials(accessKey, secretKey);
} catch (FileNotFoundException io) {
log.error("Properties file not found.");
return null;
} catch (IOException io) {
log.error("Error loading property.");
return null;
} catch (IllegalArgumentException io) {
log.error("Access Key or Secret key not present/found");
return null;
}
在jUnit中,我能够测试和覆盖成功,FileNotFoundException
和IllegalArgumentException
,但在这种情况下无法覆盖和测试IOException
。我是编写测试的新手。我也尝试过理解 mockito,但找不到处理这种情况的方法。
您可以尝试使用 PowerMockito.mockNew (https://automationrhapsody.com/mock-new-object-creation-powermock/( 来模拟创建新的属性对象。然后你可以模拟一个例外