Mockito allows developers to verify the behavior of the system under test (SUT) without establishing expectations beforehand.3 One of the criticisms of mock objects is that there is a tight coupling of the test code to the system under test.4 Mockito attempts to eliminate the expect-run-verify pattern5 by removing the specification of expectations. Mockito also provides some annotations for reducing boilerplate code.6
Mockito began by expanding on the syntax and functionality of EasyMock.78
Consider this decoupled Hello world program; we may unit test some of its parts, using mock objects for other parts.
The result of HelloApplication launching will be the following:
Unit test for HelloActable component may look like this:
It uses mock objects for the Greeter and Appendable interfaces, and implicitly assumes the next use case:
Integration test code for testing HelloActable wired together with Greeter may look like the following:
It uses mock objects only in place of Appendable interfaces, uses the real implementations for other (HelloActable and Greeter) interfaces, and implicitly assumes the next use case:
As can be seen from the import statements of HelloActionUnitTest and HelloActionIntegrationTest classes, it is necessary to put some Mockito jars and JUnit jars in your class path to be able to compile and run the test classes.
"Mockito in six easy examples". 2009. Retrieved 2012-10-05. http://gojko.net/2009/10/23/mockito-in-six-easy-examples/ ↩
"What's the best mock framework for Java?". Retrieved 2010-12-29. https://stackoverflow.com/questions/22697/whats-the-best-mock-framework-for-java ↩
"Features and Motivations". Retrieved 2010-12-29. https://code.google.com/p/mockito/wiki/FeaturesAndMotivations ↩
Fowler, Martin (2007). "Mocks Aren't Stubs". Retrieved 2010-12-29. http://martinfowler.com/articles/mocksArentStubs.html#CouplingTestsToImplementations ↩
Faber, Szczepan. "Death Wish". Retrieved 2010-12-29. https://web.archive.org/web/2010/http://monkeyisland.pl/2008/02/01/deathwish/ ↩
Kaczanowski, Tomek. "Mockito - Open Source Java Mocking Framework". Retrieved 2013-09-17. http://www.methodsandtools.com/tools/mockito.php ↩
Faber, Szczepan. "Mockito". Archived from the original on 2010-03-29. Retrieved 2010-12-29. https://web.archive.org/web/20100329033254/http://monkeyisland.pl/2008/01/14/mockito/ ↩
"Mockito Home Page". Retrieved 2010-12-29. https://code.google.com/p/mockito/ ↩