模拟 ScheduledExecutorService.scheduleWithFixedDelay(...) 返回 null

编程入门 行业动态 更新时间:2024-10-28 06:33:26
本文介绍了模拟 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

本文发布于:2023-10-31 05:22:13,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1545130.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:ScheduledExecutorService   scheduleWithFixedDelay   null

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!