2 Minutes To Code (β Version)

Mockito - 2 minute tutorial

Overview of Mockito framework

Mockito is used to mock java classes for testing purposes.

Let us understand this with an example. Suppose you write an explanation which will need to connect to DB and update tables and also hit an http server, both of which are either cumbersome to set-up or are managed by someone else. Even then, you need to be able to test your application. Mockito, or other mocking frameworks like easymock come to your rescue.

When you mock a class, you are actually creating a dummy or mock of that class, and then re-defining method to bypass the actual db connection/http connection and instead return dummy responses or objects which you are expecting the db layer/http server to return anyways. If your application works fine with these dummy objects, then when the actual db layer/http server are plugged in, the application will work as expected.

Mocking frameworks are generally used with unit testing apis like junit to test applications modularly. Check the tutorials for junit4 and junit3.