Mocking With Mockito

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
import static org.mockito.Mockito.mock;

then, you do

1
Class1 class1 = mock(Class1.class);

If you want to mock a certain method call in class1, do the following:

1
2
import static org.mockito.Mockito.when;
import static org.junit.Assert.assertEquals;

then, you do

1
2
when(class1.methodReturnsString()).thenReturn("foo");
assertEquals("foo", class2.callClass1Method());

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.

  1. git clone https://github.com/pugnusferreus/mockito_test
  2. cd mockito_test
  3. ant
  4. 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!

Copyright © 2016 - Benson Lim -