在我的单元测试中,我将ScheduledExecutoryService
类的模拟实例注入到我试图测试的类中,以便在调用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));
发布于 2017-05-04 22:55:04
您传递的是整数(可能是方法参数的定义),尽管您需要的是长值。
更改为:
Mockito.doReturn(t).when(scheduledExecutorService).scheduleWithFixedDelay(
any(Runnable.class), anyInt(), anyInt(), any(TimeUnit.class));
发布于 2018-06-09 06:12:44
您的应用程序代码正在使用scheduleAtFixedRate
,但是您的测试代码只是模拟方法scheduleWithFixedDelay
。
https://stackoverflow.com/questions/43786137
复制相似问题