Skip to content
Advertisement

Mocking a Spy method with Mockito

I am writing a unit test for a FizzConfigurator class that looks like:

JavaScript

I’d like to write a simple unit test that stubs the doWidget(String,Config) method (so that it doesn’t actually fire and hit the database), but that allows me to verify that calling doBuzz(String) ends up executing doWidget. Mockito seems like the right tool for the job here.

JavaScript

This seems like a good gameplan (to me at least). But when I actually go to code it up, I get the following compiler error on the 2nd line inside the test method (the Mockito.when(...) line:

The method when(T) in the type Mockito is not applicable for the arguments (void)

I see that Mockito can’t mock a method that returns void. So I ask:

  1. Am I approaching this test setup correctly? Or is there a better, Mockito-recommended, way of testing that doBuzz calls doWidget under the hood? And
  2. What can I do about mocking/stubbing doWidget as it is the most critical method of my entire FizzConfigurator class?

Advertisement

Answer

I wouldn’t use exceptions to test that, but verifications. And another problem is that you can’t use when() with methods returning void.

Here’s how I would do it:

JavaScript
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement