blog.smart-java.nl
Ordina J-Technologies – Java Blog



Mockito

By: Sander Abbink, 15 January 2010

If you want to test objects in isolation it is often usefull to use Mocks. Here is how you can do this with the Mockito framework.

First the maven dependency:

 org.mockito
  mockito-all
  1.8.1

If you don’t use a Maven plugin in let’s say Eclipse, then you can create a directory testlib which contains the jar. Add this jar to the buildpath (this won’t affect the Maven build).

It is also handy to use the following static imports:

   import static org.mockito.Mockito.*;
   import static org.junit.Assert.*;

Tip for Eclipse: if you want to prevent organize imports (ctrl+shift+o) to resolve the static imports: java -> code style -> organize imports -> Number of static imports needed for (set to 1).

Creating a Mock object:

   List mockedlist = mock(ArrayList.class);
   mockedlist.add("Hello");
   String value = mockedlist.get(0);
   mockedlist.get(5);

All the methods in ArrayList are mocked. So the String “Hello” won’t actually be added to the List (e.g. value == null). Also get(5) won’t throw an IndexOutOfBoundsException.

You can also stub method calls, like this:

   List mockedlist = mock(ArrayList.class);
   when(mockedlist.get(0)).thenReturn("Hello");

Or verify invocations:

   String value = "Hello";
   verify(mockedlist).get(0);
   verify(mockedlist).add(eq(value));
   verifyNoMoreInteractions(mockedlist);

eq is a method in the Matchers class. It will verify that an object is added to the mocked List which is equal to the String “Hello”. If you use a Matcher for one argument in a method, then you have to use a Matcher for all the other arguments too.

See javadoc for the other Matchers methods http://mockito.googlecode.com/svn/branches/1.5/javadoc/org/mockito/Matchers.html

You can also write your own Matcher, although this is rarely neccesary. An example:

class IsStringEqualButNotSame extends ArgumentMatcher {
    private String originalString;

    public IsStringEqualButNotSame (String originalString) {
	    this.originalString= originalString;
    }

    public boolean matches(Object value) {
	    return ((String)value).equals(originalString) && value != originalString;
    }
}
    String value = "Hello";
    verify(mockedlist).add(argthat(new IsStringEqualButNotSame(value));

This Matcher will match if a String is logically equal but the object is not the same. Not the best example :) but you can make Matchers this way.

Spying on real objects. You can spy a real object to verify interactions or to stub only one particular method. An example:

  List myList = new ArrayList<>()
  List myListSpy = spy(myList);
  myListSpy.get(5);    // will throw IndexOutOfBoundsException

  when(myListSpy.get(5)).thenReturn("Hello"); // will throw IndexOutOfBoundsException

If you stub a method like the example above the real method will still be called. You must use a slightly different syntax for this:

    doReturn("Hello").when(myListSpy.get(5));

Additional sources:

  • Mockito site
  • Mockito vs EasyMock
  • Not really related to Mockito. If you annotate a method with @Before this method will be called before every unittest in the class.
    If you annotate a method with @BeforeClass this method will only be called once. This method must be static.

    2 reacties op “Mockito”

    1. Roy van Rijn zegt:

      We use it all the time, and I love this tool. It only takes a short time to get used to if you are already familiar with EasyMock.

    2. Jan-Kees van Andel zegt:

      Yeah, it’s great. Especially the way verification works is way more intuitive than with EasyMock.

      It also has a great API. It really reads like proper English.

    Laat een reactie achter