Last year after finally getting fed up with mockobjects, I decided to have a look around for a better alternative. Having used EasyMock in the past, I knew there was at least one better framework. But after talking to other Atlassians I was pointed to Mockito (which started as a fork of EasyMock). Mockito finally got rid of the record-replay concept which was annoying me so much while using EasyMock, and after going through the docs and playing a little bit around with it, I was convinced pretty quickly that this is the best mocking framework currently available for Java.

Why is it better?

I gave a presentation on Mockito a while ago which included a short comparison with other mocking frameworks. Here is a brief summary of the improvements over other mocking frameworks:

  • No Strings for method identification but plain java code (gives you code completion)
  • Refactoring safe
  • Type safe (uses Java 5 Generics)
  • Overall less code and more readable

Code improvements

A couple of things I noticed while doing the conversion:

  • Don’t stub and verify too much
    Mockito has the very nice default behaviour of just returning the return types default value if you don’t stub a method. And it doesn’t fail if you call a void method which has no expectation associated. That way you can save yourself from writing a lot of unneccessary setup code, and just setup the stuff you really care about.
  • Stubbing and expectations in one block
    All other mocking frameworks I know require you to setup stubs and expectations before calling the method under test. That makes the code sometimes hard to read. Mockito forces you to setup your stubs before calling the method under test and only verify your expectations after that. That feels way more natural and makes the code easier to read.
  • Calling verify where there is nothing to verify
    Other mocking frameworks distinguish between setting up the expectations and actually calling verify(). I saw a lot of places where people call verify() even though there were no expectations setup. With Mockito a verify always comes with the expectation, which forces you to get rid of all those no-op verify() calls which just clutter the code.

Writing tests is fun again

The biggest improvement of all for me is the fact that I actually have fun again writing unit tests. Well, there are definitely more fun things than writing tests, but at least I don’t have to shudder and get goosebumps when I have to write a unit test 🙂
And from the feedback I got from the rest of the Confluence team everyone else feels the same, which should eventually lead to people writing more tests again and actually maintain them.

Consider moving to Mockito

I know that a couple of people on other teams here at Atlassian aren’t to happy with their current mocking framework either. So if you think about moving give Mockito a shot. We are using it now for more than two months for Confluence and we haven’t discovered any issues yet.

How to make writing unit tests fun again (Mockito to the rescue)