What's new? | Help | Directory | Sign in
Google
mockito
Mocking java simpler & better
  
  
  
  
    
Join project
Project owners:
  szczepiq
Project members:
iczechowski

Mockito - mock objects library for java

Java mocking is dominated by expect-run-verify libraries like EasyMock or jMock. Mockito offers simpler and more intuitive approach: you ask questions about interactions after execution. Using mockito, you can verify what you want. Using expect-run-verify libraries you are often forced to look after irrelevant interactions.

No expect-run-verify also means that Mockito mocks are often ready without expensive setup upfront. They aim to be transparent and let the developer to focus on testing selected behavior rather than absorb attention.

Mockito has very slim API, almost no time is needed to start mocking. There is only one kind of mock, there is only one way of creating mocks. Just remember that stubbing goes before execution, verifications of interactions go afterwards. You'll soon notice how natural is that kind of mocking when test-driving java code.

Mockito has similar syntax to EasyMock, therefore you can refactor safely. Mockito doesn't understand the notion of 'expectation'. There is only stubbing and verifications.

Mockito implements what Gerard Meszaros calls a Test Spy.

Some other features:

Getting started with Mockito 1.3

Put mockito-all-1.3.jar on classpath.

Maven Users

Sample verification

 import static org.mockito.Mockito.*;
 
 //mock creation
 List mockedList = mock(List.class);
 
 //using mock object - will never throw any unexpected exception!
 mockedList.add("one");
 mockedList.clear();
 
 //verification - if fails then it will throw an assertion error here:
 verify(mockedList).add("one");
 verify(mockedList).clear();

Sample stubbing

 //You can mock concrete classes, not only interfaces
 LinkedList mockedList = mock(LinkedList.class);
 
 //stubbing - before execution
 stub(mockedList.get(0)).toReturn("first");
 
 //following prints "first"
 System.out.println(mockedList.get(0));
 
 //following prints "null" because get(999) was not stubbed
 System.out.println(mockedList.get(999));

More examples

Why to build yet another one?

Ideas? Suggestions? Problems?

If you have any suggestions, find documentation unclear or you found a bug, write here:

http://groups.google.co.uk/group/mockito

Limitations

Credits

Hats down before EasyMock folks for their ideas on beautiful and refactorable mocking syntax. Technically, Mockito is an EasyMock fork. It keeps (read: refactors) stuff like invocation matching and mock creation but rewrites the rest (like verification, stubbing, etc.)

Some other mocking frameworks share similar concepts as Mockito:

Here are just some of my friends who contributed ideas to Mockito (apologize if I missed somebody): Igor Czechowski, Patric Fornasier, Jim Barritt, Felix Leipold, Liz Keogh

Special thanks to Erik Ramfelt for putting Mockito on his Hudson server (continuous integration).

Updates on this page

I'm not using 'record-replay-verify' term any more because it's not what jMock does. Instead I use 'expect-run-verify' which is the pattern of both jMock and EasyMock.