Wednesday, 19 September 2012

TDD with Mockito

You may not be getting the most out of your TDD and JUnit, Mockito is an effective mocking framework that works great.

Mockito Verify

Verify allows you to set up expectations on the mock methods that are 'probed' by the test case in a whitebox manner.
public void testMockitoVerify()  
    //mock creation  
    List<String> mockedList = mock(List.class);  
    //using mock object  

Mockito Stubbing (toReturn, toThrow)

Mockito also supports stubs by allowing you to specify return values when certain methods are called. Once stubbed, the method will always return stubbed value regardless of how many times it is called.
public void testMockitoStubbing()  
     //You can mock concrete classes, not only interfaces  
     LinkedList mockedList = mock(LinkedList.class);  
     stub(mockedList.get(1)).toThrow(new RuntimeException());  
     //following prints "first"  
     //following prints "null" because get(999) was not stubbed  

Mockito Invocations (times, atLeast, atMost)

You can verify a certain number of calls by using times for an exact number of calls, atMost for upto a number and atLeast for above a number. 
public void testMockitoInvocations()  
    //mock creation  
    List<String> mockedList = mock(List.class);  
    //using mock  
    mockedList.add("three times");  
    mockedList.add("three times");  
    mockedList.add("three times");  
    //verification, times(1) is used by default  
    //verification using never(). never() is an alias to times(0)  
    verify(mockedList, never()).add("never happened");  
    //verification using atLeast()  
    verify(mockedList, atLeastOnce()).add("three times");  
    verify(mockedList, times(3)).add("three times");  
    //verify using atMost  
    verify(mockedList, atMost(3)).add("three times");  

Mockito Whitebox (when / thenReturn)

You can specify mock return values using when and thenReturn.
public void testMockitoWhitebox() {  
    //mock creation  
    Calendar mock = mock(Calendar.class);  
    TimeZone tz = mock(TimeZone.class);  
    String result = mock.getTimeZone().getID();  
    assertEquals("PST", result);  

Mockito Throws

You can set up exception throwing behaviour for you mocks using doThrow.  
@Test(expected = RuntimeException.class)   
public void testMockitoThrows() {  
    //mock creation  
    List<String> mockedList = mock(List.class);  
    doThrow(new RuntimeException()).when(mockedList).clear();        

Mockito Verification Equals

You can verify mock method parameters using eq.
public void testMockitoEq() {  
    //mock creation  
    List mock = mock(List.class);  

Mockito Verification Any

If you do not care to verify a specific parameter values being passed to a mock you can use 'any'.
public void testMockitoAny() {  
    //mock creation  
    List<String> mockedList = mock(List.class);  

Mockito Verification ArgThat

If you want to use a custom matcher to verify an argument then you can use argThat.
public void testMockitoArgThat() throws IOException{          
    OutputStream mock=mock(OutputStream.class);  
    OutputStreamWriter osw=new OutputStreamWriter(mock);  
    // can't do this as we don't know how long the array is going to be  
    // verify(mock).write(new byte[]{'a'},0,1);  
    BaseMatcher arrayStartingWithA=new BaseMatcher(){  
        public void describeTo(Description description) {  
            // nothing  
        // check that first character is A  
        public boolean matches(Object item) {  
            byte[] actual=(byte[]) item;  
            return actual[0]=='a';  
    // check that first character of the array is A, 
    // and that the other two arguments are 0 and 1  
    verify(mock).write((byte[]) argThat(arrayStartingWithA), eq(0),eq(1));      

You can get Mockito here.
Example source here.

No comments:

Post a Comment