java - 调用 void 方法的 CompletableFuture.runAsync() 的 Mockito 测试用例
问题描述
我需要帮助为以下方法编写模拟测试用例。
public void getCouponAndNotifyAsync(String countryId, String channelId,
String storeNumber, String clientId, NotificationRequest notificationRequest)
throws FirestoreException, TurneroServiceException {
CompletableFuture.runAsync(() -> getCouponAndNotify(countryId, channelId,
storeNumber, clientId, notificationRequest));
}
其中 getCouponAndNotify() 是一个 void 方法。
在下面尝试但它不起作用
@Test
public void getCouponAndNotifyAsync() throws Exception {
//doNothing().when(turneroService).getCouponAndNotify(COUNTRYID, CHANNELID, STORENUMBER, CLIENTID, new NotificationRequest("ext_rborse@falabella.cl", "all"));
CompletableFuture<Void> runAsync = CompletableFuture
.runAsync(() -> doNothing().when(turneroService).getCouponAndNotify(COUNTRYID, CHANNELID, STORENUMBER, CLIENTID, new NotificationRequest("ext_rborse@falabella.cl", "all")));
assertTrue(runAsync.isDone());
}
更新了测试用例,但仍然无法正常工作。
@Test
public void getCouponAndNotifyAsync() throws Exception {
//doNothing().when(turneroService).getCouponAndNotify(COUNTRYID, CHANNELID, STORENUMBER, CLIENTID, new NotificationRequest("ext_rborse@falabella.cl", "all"));
CompletableFuture<Void> runAsync = CompletableFuture
.runAsync(() -> doNothing().when(turneroService).getCouponAndNotify(COUNTRYID, CHANNELID, STORENUMBER, CLIENTID, new NotificationRequest("ext_rborse@falabella.cl", "all")));
assertTrue(ForkJoinPool.commonPool().awaitQuiescence(5, TimeUnit.SECONDS));
assertTrue(runAsync.isDone());
}
解决方案
我假设您正在getCouponAndNotify()
其他地方进行测试,因此您不必担心它会引发异常。
您将遇到的是getCouponAndNotifyAsync()
和getCouponAndNotify()
返回之间的竞争条件。有几个解决方案:
由于您使用的是 common ForkJoinPool
,请执行
assertTrue(ForkJoinPool.commonPool().awaitQuiescence(5, TimeUnit.Seconds));
或者,您可以注入 anExecutorService
并将其用作supplyAsync()
. 你有几个选择:你可以使用一个模拟,你可以使用一个与当前线程一起运行的ExecutorService
,或者你可以注入一个标准,然后在你的测试中调用和。Executors.newSingleThreadExecutor()
shutdown()
awaitTermination()
你也可以返回一个你可以CompletionStage<Void>
在getCouponAndNotifyAsync()
测试中等待的。
推荐阅读
- python - 使用模板标签设置“id”属性
- javascript - Bootstrap Modal 不会在 Chrome 和 Safari 上关闭
- html - Laravel Bootstrap CSS 链接 {{ 资产 }}
- bash - bash IFS awk 2 美元
- git - 'git svn dcommit' 提交乱序是否安全
- r - R将列表合并为一个
- java - 我自己的 MySQL 服务器 SQLNonTransientConnectionException 的 Java SQL 错误
- python - 为什么我向 template.cvs 添加值的方式最多只添加 4 行?
- bayesian-networks - Pomegranate 中的贝叶斯网络:ValueError:样本与模型的维数不同
- javascript - 这段用于同位素过滤器的 Javascript 是做什么的?