Mockito

15 January 2008

My colleague Szczepan has just released a new mocking library called Mockito (I leave it up to your imagination to picture the situation in which he came up with the name...).

Essentially, Mockito is built upon EasyMock but seems to provide a more elegant approach and simpler API to testing using mock objects than the latter. For example, I always found the EasyMock's recording and replaying stages quite cumbersome. Not suprisingly, I'm thus really glad to see that Szczepan came up with a simpler solution in which stubbing and verification are separated into different steps. This means that you stub your objects first, then use them, and finally verify only the interactions (with the mock objects) that you're interested in.

The following sample code is from the Mockito project site:
import static org.mockito.Mockito.*;

//let's mock a List!
List mockedList = mock(List.class);

//stubbing
stub(mockedList.get(0)).toReturn("first element");

//using mock object
mockedList.add("one");
mockedList.clear();

//verification
verify(mockedList).add("one");
verify(mockedList).clear();


I really look forward to using it more and finding out if it indeed makes testing easier. It surely does look promising =)