首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >模拟ScheduledExecutorService.scheduleWithFixedDelay(...)正在返回null

模拟ScheduledExecutorService.scheduleWithFixedDelay(...)正在返回null
EN

Stack Overflow用户
提问于 2017-05-04 22:47:27
回答 2查看 5.3K关注 0票数 3

在我的单元测试中,我将ScheduledExecutoryService类的模拟实例注入到我试图测试的类中,以便在调用scheduleAtFixedRate(...)方法时,它返回一个模拟的Future。但是,由于某些原因,它总是返回null。有什么想法吗?

应用程序代码:

代码语言:javascript
复制
Future<?> t = 
scheduledExecutorService.scheduleAtFixedRate(this, 10, 10, TimeUnit.SECONDS);

测试代码:

代码语言:javascript
复制
@Mock ScheduledExecutorService scheduledExecutorService;
@Mock ScheduledFuture<?> t;

Mockito.doReturn(t).when(scheduledExecutorService).scheduleWithFixedDelay(
any(Runnable.class), anyLong(), anyLong(), any(TimeUnit.class));
EN

回答 2

Stack Overflow用户

发布于 2017-05-04 22:55:04

您传递的是整数(可能是方法参数的定义),尽管您需要的是长值。

更改为:

代码语言:javascript
复制
Mockito.doReturn(t).when(scheduledExecutorService).scheduleWithFixedDelay(
any(Runnable.class), anyInt(), anyInt(), any(TimeUnit.class));
票数 5
EN

Stack Overflow用户

发布于 2018-06-09 06:12:44

您的应用程序代码正在使用scheduleAtFixedRate,但是您的测试代码只是模拟方法scheduleWithFixedDelay

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/43786137

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档