模拟 ScheduledExecutorService.scheduleWithFixedDelay(...) 返回 null [英] Mocking ScheduledExecutorService.scheduleWithFixedDelay(...) is returning null

查看:70
本文介绍了模拟 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆