Mockito gotcha

I recently had a bit of a shock while reviewing someone’s code, finding the following line in one of their unit tests:

verify().that(sut.update());

Where: sut is their System Under Test and verify is the part of the Mockito Flex framework.

Exercise for the reader: What’s wrong with this picture?

The answer: You cannot expect Mockito to verify that something was called on anything that isn’t a mock! How the hell is it supposed to know?

At least, if attempting to stub a method of a non-mock, you’ll get a handy error telling you not to be so damn silly. But, in this case, the verify will always work. So the test will pass, but it isn’t actually verifying anything! Where do I start with the bad? This is the worst kind of test, since it provides a false sense of security on the robustness of a system. Thankfully, all the tests in this codebase had called their System Under Test either sut, _sut or SUT, so it was pretty easy to get Hudson to mark a build as unstable if it finds such madness.

Leave a Reply