本文介绍了模拟 ScheduledExecutorService.scheduleWithFixedDelay(...) 返回 null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在我的单元测试中,我已将 ScheduledExecutoryService 类的模拟实例注入到我要测试的类中,以便 scheduleAtFixedRate(...) 方法被调用,它返回一个模拟的 Future.但出于某种原因,它总是返回 null.有什么想法吗?
In my unit test, I've injected a mocked instance of the ScheduledExecutoryService class into the class that I'm trying to test so that when the scheduleAtFixedRate(...) method is called, it returns a mocked Future. For some reason though, it's always returning null. Any ideas ?
应用代码:
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 值,但您正在传递整数(这可能是方法参数的定义).
You are passing Integers (and probably that is the definition of the method parameters) though you are expecting Long values.
更改为:
Mockito.doReturn(t).when(scheduledExecutorService).scheduleWithFixedDelay( any(Runnable.class), anyInt(), anyInt(), any(TimeUnit.class));更多推荐
模拟 ScheduledExecutorService.scheduleWithFixedDelay(...) 返回 null
发布评论