在我的单元测试中,我将 ScheduledExecutoryService
类的模拟实例注入(inject)到我要测试的类中,这样当 scheduleAtFixedRate(...)
方法被调用,它返回一个模拟的 Future
。但出于某种原因,它总是返回 null
。有什么想法吗?
申请代码:
Future<?> t =
scheduledExecutorService.scheduleAtFixedRate(this, 10, 10, TimeUnit.SECONDS);
测试代码:
@Mock ScheduledExecutorService scheduledExecutorService;
@Mock ScheduledFuture<?> t;
Mockito.doReturn(t).when(scheduledExecutorService).scheduleWithFixedDelay(
any(Runnable.class), anyLong(), anyLong(), any(TimeUnit.class));
最佳答案
尽管您期望 Long 值,但您正在传递 Integers(这可能是方法参数的定义)。
更改为:
Mockito.doReturn(t).when(scheduledExecutorService).scheduleWithFixedDelay(
any(Runnable.class), anyInt(), anyInt(), any(TimeUnit.class));
关于java - 模拟 ScheduledExecutorService.scheduleWithFixedDelay(...) 返回 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43786137/