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 mockito-1.2.zip contains all jars, javadoc and source.

4 comments:

Handerson Frota said...

Olha ai :D finalmente :D rsrsrs

Ta massa o post, parabéns.

PS: Dica: Troca essa funcionalidade de post pq é meio chato :P sério rsrsr

Anonymous said...

Well, good post Rene, but where is code samples of this framework? :)

You could show us some code sample!

renearaujo said...

Sure, in next post :) thanks!

stacy said...

I like this blog lots of useful info. I think this is great info on a home movie theater system if you need one.