Mock void methods with mockito

Long time no see dear internet. We are not dead yet, just very busy. Which is kind of a good thing I guess.

I recently started using Mockito in my projects. Mockito enables you to very easily mock stuff in your unit tests. However I got stuck when I was about to test a method which calls the persist and refresh-method of the EntityManager. As you will know, or notice, is that the refresh method modifies the given argument and doesn’t return anything.
So now I needed to mock a void method, which modifies the argument instead of returning it.

The solution is suprisingly simple the Answer-Interface:

Et voilà, now we can mock methods that modify their arguments very easily.

 

Leave a Reply

Your email address will not be published.