【发布时间】:2019-08-18 20:17:36
【问题描述】:
我需要帮助为以下方法编写模拟测试用例。
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());
}
【问题讨论】:
-
您是否尝试使用partial mock?您最好在测试中扩展类并覆盖同步方法。
-
你在哪里打电话
getCouponAndNotifyAsync()在你的测试?我也不认为 mock 不像你想象的那样工作。 -
你能解释一下吗? getCouponAndNotifyAsync() 在控制器类中被调用。该测试通过了。
标签: java spring-boot junit java-8 mockito