|
|
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:
- 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.3.jar includes cglib, objenesis and java source
- Zip distribution mockito-1.3.zip contains all jars, javadoc and source
- Release Notes
Getting started with Mockito 1.3
Put mockito-all-1.3.jar on classpath.
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
- Cannot mock final classes
- Cannot mock final methods - their normal behavior is executed
- Cannot mock static methods
- Cannot mock equals(), hashCode() - those are always implemented by mocks
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.
