【发布时间】:2018-04-23 18:22:31
【问题描述】:
我想在这个来源中模拟repository.actionOnFile(String path, Consumer<InputStream> action):
@Autowired
private FileRepositoryService repository;
public Document getDocument(URL url) {
MutableObject<Document> obj = new MutableObject<>();
Consumer<InputStream> actionOnFile = inputStream -> obj.setValue(getDocument(inputStream));
try {
repository.actionOnFile(url.toExternalForm(), actionOnFile);
} catch (S3FileRepositoryException e) {
throw e.getCause();
}
return obj.getValue();
}
问题在于第二个参数是一个 lambda 表达式。
如何用mockito模拟它,我需要将输入流传递给accept方法来测试它吗?
【问题讨论】:
-
您可能希望重构您的服务以接受
Function<InputStream, R>并返回R。我假设您成为消费者之后关闭输入流,但您的问题正是当您的服务依赖某些副作用来产生任何结果时会发生什么。