Image by Tatiana Popova, licensed via Shutterstock

Don’t Verify That Mock — Yet!

Using a Delay When Verifying a Method Call on a Mock In Mockito

Kenneth Kousen
6 min readMay 24, 2023

--

https://pragprog.com/newsletter/

I was catching up on Twitter as I’m awaiting that platform’s inevitable demise, when I ran across this post by Maciej Walkowiak:

Tweet by Maciej Walkowiak about using a timeout in Mockito’s verify method.

Maciej included a code snippet in his tweet, which I would include here, but instead, I’m going to present a slightly modified version.

I recently wrote a whole book about Mockito, called Mockito Made Clear (from your friendly, neighborhood Pragmatic Programmers), and I never noticed a timeout argument on the static verify method from the Mockito class, as Maciej presented in his tweet. This post shows how to use it.

The way the verify method appears in the Javadocs is:

There’s a version of the method that takes only a single argument: the mock object. Then the…

--

--

Kenneth Kousen
The Pragmatic Programmers

Author of the books Mockito Made Clear, Help Your Boss Help You, Kotlin Cookbook, Modern Java Recipes, Gradle Recipes for Android, and Making Java Groovy