If you want to write a unit test which verifies that something implements a particular interface, or extends a particular class, here’s how…
Continue reading How to verify that something implements an interface with Mockito
If you want to write a unit test which verifies that something implements a particular interface, or extends a particular class, here’s how…
Continue reading How to verify that something implements an interface with Mockito
You need to use some kind of Dependency Injection (DI for short) to provision some System Under Test (SUT for short) in tests you’re writing, but can’t or don’t want to use a DI framework to do so.
Alternative titles for this post include:
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.