Sunday, March 23, 2008

Mockito: A Test Spy implementation

What's the best Mock Framework that you think?

Some people say: "EasyMock, sure!".

Others: "JMock!".

Wait wait wait! You be heard about Test Spy?

Mockito is an implementation of Test Spy, but doesn't use this metaphor at all. Created by Szczepan Faber, aiming to become the best mocking framework!

So, what Mockito do?

Like expect-run-verify library, as EasyMock and Jmock, Mockito offers simpler and more intuitive approach. You ask questions about interactions after execution. Using other libraries, you often look irrelevant interactions.

Mockito has similar syntax to EasyMock, therefore you can do refactor safely! Differ by notion of expectations, there is only stubbing or verification.

Some other features:

* Mocks concrete classes as well as interfaces
* Allows flexible verification in order (e.g: verify in order what you want, not every single interaction)
* Supports exact-number-of-times and at-least-once verification
* Flexible verification or stubbing using argument matchers (anyObject(), anyString() or refEq() for reflection-based equality matching)
* Allows creating custom argument matchers or using existing hamcrest matchers
* Verification errors are clean - click on stack trace to see failed verification in test; click on exception's cause to navigate to actual interaction in code. Stack trace is always clean.
* Little annotation syntax sugar - @Mock
* Single-jar distribution mockito-all-1.2.jar includes cglib, objenesis and java source
* Zip distribution contains all jars, javadoc and source.