mockito and CompletableFuture

12,929 views
Skip to first unread message

Christoph Sturm

unread,
Feb 17, 2016, 12:43:24 PM2/17/16
to mockito
I'm working with an api that uses a lot of CompletableFutures and somehow the combination of futures and mocks does not work so well for me yet.

Things could be much easier if 
* mockito would return a CompletableFuture.completedFuture as default value when no expectations are set up.
* there was support for returning a list of futures.

when(blah.getSeppl()).thenReturnFuture(); // this would return a new future for multiple invocations of the method
....
verifyFutures(blah).getSeppl().get(0).wasJoined();

this is just a raw idea, any comments?

regards
 chris

Marcin Zajączkowski

unread,
Feb 17, 2016, 5:59:33 PM2/17/16
to moc...@googlegroups.com
CompletableFuture was introduced in Java 8. As the current line of
Mockito 2.x is Java 7 compatible there seems not to be a (simple*) way
to cover default answer for CompletableFuture, Optional, etc in Mockito
2 distribution.

Nevertheless Mockito has a mechanism to override class used to create
default return values for the whole project (see my old blog post [1]).
Using it you could implement your own Answer with CompletableFuture
support delegating other types to ReturnsMoreEmptyValues or
ReturnsSmartNulls.

In fact the idea has been in my head for some time and you motivated me
to do the initial implementation in mockito-java8 - see changes in the
defaultAnswers branch [2]. You can use the idea in your project or
create a PRs in mockito-java8 (WARNING. The implementation is definitely
a subject to change).

[1] -
https://solidsoft.wordpress.com/2012/07/02/beyond-the-mockito-refcard-part-1-a-better-error-message-on-npe-with-globally-configured-smartnull/
[2] - https://github.com/szpak/mockito-java8/tree/feature/defaultAnswers

Alternatively, there is a new hope - Mockito 3 :) -
https://github.com/mockito/mockito/issues/297

Btw, Unfortunately I'm not sure what you would like to achieve in the
second case with "a list of futures".

* - runtime class detection and reflection to instantiate it makes code
less readable and more error prone

Marcin

--
http://blog.solidsoft.info/ - Working code is not enough

Christoph Sturm

unread,
Feb 18, 2016, 3:04:08 AM2/18/16
to moc...@googlegroups.com
wow thanks that was quick!

the idea with “a list of futures” was that I want to mock a method to return a unique future every time, and then in the verify step i want to check what was done to those futures.
> --
> You received this message because you are subscribed to a topic in the Google Groups "mockito" group.
> To unsubscribe from this topic, visit https://groups.google.com/d/topic/mockito/rUydi2DkyLs/unsubscribe.
> To unsubscribe from this group and all its topics, send an email to mockito+u...@googlegroups.com.
> To post to this group, send email to moc...@googlegroups.com.
> Visit this group at https://groups.google.com/group/mockito.
> To view this discussion on the web visit https://groups.google.com/d/msgid/mockito/56C4FB51.9030300%40wp.pl.
> For more options, visit https://groups.google.com/d/optout.

Eric Lefevre-Ardant

unread,
Feb 18, 2016, 3:27:52 AM2/18/16
to moc...@googlegroups.com
Well, you could do that with Answers. Something like that:

  doAnswer(new Answer() {
      public Object answer(InvocationOnMock invocation) {
          return CompletableFuture.completedFuture();
      }})
  .when(mock).someMethod(any());

If you need to check the actual CompletableFuture instance, then maybe you want to instantiate CompletableFuture before declaring the Answer.

I don't think there is anything special with CompletableFuture in the context of Mockito.

You received this message because you are subscribed to the Google Groups "mockito" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mockito+u...@googlegroups.com.

To post to this group, send email to moc...@googlegroups.com.
Visit this group at https://groups.google.com/group/mockito.
Reply all
Reply to author
Forward
0 new messages