At movideo, we’re using Mockito as our mocking framework. Mockito provides a clean and simple API to mock your Java objects.
Let’s say Class1 has a couple of arguments for it’s constructor and you do not want to provide each arguments, you can simply do:
1
|
|
then, you do
1
|
|
If you want to mock a certain method call in class1, do the following:
1 2 |
|
then, you do
1 2 |
|
Assuming that class2.callClass1Method()
will call
class1.methodReturnsString()
the assertion will be successful.
To get a better picture of what Mockito can do for you, you can checkout my Mockito Test project from my Github Sample Project.
I’ll assume that you have Git, Java 1.6 and Ant installed on your machine.
git clone https://github.com/pugnusferreus/mockito_test
cd mockito_test
ant
- Open docs/unitTest/index.html in your browser
You can see that all the unit tests pass.
Thanks @cstrzadala for introducing Mockito to all of us!