How to combine verifyNoInteractions with timeout

21 views
Skip to first unread message

Nico Van Belle

unread,
May 10, 2021, 9:19:52 AM5/10/21
to mockito

I have a piece of java code that runs in a CompletableFuture#runAsync block and I want to verify that in this block, a specific service is never called.

Normally to verify that it is actually called, I can use the timeout VerificationMode to assert when the runAsync is likely completed

verify(myService, timeout(500)).methodX(param);

But in my case I want to check if the service had no interactions. Normally this is done with verifyNoInteractions(myService); but this fires immediately and has a high chance for false positives.

The workaround is that, for every method in the service, I type use the times(0) VerificationMode e.g.:

verify(myService, timeout(500).times(0)).methodX(param); 
verify(myService, timeout(500).times(0)).methodY(param); 
verify(myService, timeout(500).times(0)).methodZ(param);

But this is an awful idea for all obvious reasons.

I know another possibility would be adding Thread.sleep(500); but I want to use the Mockito API if possible. 

I just have not seen it done elsewhere.


Thank you for your support!

Reply all
Reply to author
Forward
0 new messages