如何使用Mockito模拟ExecutorService调用

huangapple go评论76阅读模式
英文:

How to mock ExecutorService call using Mockito

问题

我想使用Mockito来模拟以下代码片段。

Future<Optional<List<User>>> getUser =
       executorService.submit(() -> userRepository.findById(user.getUserId()));

我已经尝试了以下代码,但它没有起作用。

@Mock
private ExecutorService executorService;

@Mock
private UserRepository userRepository;

when(executorService.submit(() -> userRepository.findById(USER_ID)))
           .thenReturn(ConcurrentUtils.constantFuture(userList));

有人能给我一个解决方案吗?

英文:

I want to mock the following code snippet using Mockito.

Future&lt;Optional&lt;List&lt;User&gt;&gt;&gt; getUser =
       executorService.submit(() -&gt; userRepository.findById(user.getUserId()));

I have tried with the following code, but it didn't work

    @Mock
    private ExecutorService executorService;

    @Mock
    private userRepository userRepository;

    when(executorService.submit(() -&gt; userRepository.findById(USER_ID)))
           .thenReturn(ConcurrentUtils.constantFuture(userList));

Can anyone give me a solution for this?

答案1

得分: 4

感谢您的回答。我已经找到了这种情况的解决方案。

我们可以使用以下代码片段来模拟执行者服务调用。

when(executorService.submit(any(Callable.class)))
      .thenReturn(ConcurrentUtils.constantFuture(userList()));

如果在您的方法调用中有多个 ExecutorService 调用,您可以通过将它们作为逗号分隔的列表添加到Mockito调用中来模拟每个响应,如下所示。

when(executorService.submit(any(Callable.class)))
      .thenReturn(ConcurrentUtils.constantFuture(userList()),
           ConcurrentUtils.constantFuture(Optional.of(departmentList())));
英文:

Thanks for your answers. I have found a solution for this scenario.

We can mock executor service call using the following code snippet.

when(executorService.submit(any(Callable.class)))
      .thenReturn(ConcurrentUtils.constantFuture(userList()));

And if you have more than one ExecutorService calls in your method call, you can mock every response by adding them as a comma-separated list to the Mockito call as follows.

when(executorService.submit(any(Callable.class)))
      .thenReturn(ConcurrentUtils.constantFuture(userList()),
           ConcurrentUtils.constantFuture(Optional.of(departmentList())));

答案2

得分: 3

你不想纠缠于 ExecutorService 本身,而是要模拟 findById 方法以获取结果。只要模拟返回结果立即返回(除非你让它 Thread.sleep 一段时间),ExecutorService 中的调用本身很快,因此结果被包装在 Future 中。

Mockito.when(userRepository.findById(Mockito.any())).thenReturn(userList);

然后你完全不需要模拟 ExecutorService,你想要使用一个真实的服务,否则它将无法完成其预期功能。

英文:

You don't want to fiddle with the ExecutorService itself but rather mock findById to get the result. As long as the mock returns the result immediately (unless you let it Thread.sleep for a while) the call itself in the ExecutorService is quick and the result is hence wrapped inside the Future.

Mockito.when(userRepository.findById(Mockito.any()).thenReturn(userList);

Then you don't need to mock the ExecutorService at all, you want to use a real service, or else it doesn't do what it's supposed to do.

huangapple
  • 本文由 发表于 2020年10月21日 22:45:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/64466093.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定