Wednesday, September 14, 2011

Why and how to use Mockito

Mockito is an open source testing framework for Java. This framework allows creation of “Mock Objects” during Unit testing for the purpose of Test Driven Development. It helps you to create Mock Objects and define behaviour of these objects so you can use them as per your requirement in your test cases
Main Website for Mockito
Mockito Example
To run Mockito example you should have mockito jar in your project class path. In my example I am using Maven to build project
Sample Class Source Code       
import static org.junit.Assert.assertEquals;import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.junit.Test;

public class MockitoTestCases {

 public void example1() {

   * A Simple Example where you we have used VERIFY. Test Result will be
   * successful if we verify with 1 Else test result will be failure
  List mockedList = mock(List.class);
  // This will make test result successful
  // This will make test result failure
  // verify(mockedList).add("2");

  // This will make test fail
  // verify(mockedList).get(101);

  // Here we are mocking an Iterator
  Iterator ii = mock(Iterator.class);;
  // This will make test result failure because notify method is not
  // called for this object
  // verify(ii).notify();

  // Verify Times
  List mockedList1 = mock(List.class);

  verify(mockedList1, times(2)).add("1");
  // This will make result failure becuase add is only called 2 times
  // verify(mockedList1, times(3)).add("1");

  // Here We are mocking an Object behaviour by specifying WHEN clause
  ArrayList<String> mockedList2 = mock(ArrayList.class);




  System.out.println(mockedList2.size()); // return 0
  System.out.println(mockedList2.size());// return 100

   * Create a ArrayList using Mockito. If some one will inquire 99th
   * element it will return results else it will return null value

  ArrayList<String> mockObject = mock(ArrayList.class);
  when(mockObject.get(99)).thenReturn("I am 99th Object");

  System.out.println(mockObject.get(0)); // return null
  System.out.println(mockObject.get(99)); // return I am 99th Object

  System.out.println(mockObject.size()); // return 0
  System.out.println(mockObject.size()); // return 10
  for (int i = 0; i < mockObject.size(); i++) {
   System.out.println("I am priting i:" + i);
  // but if you write mockObject.get(i) it will return null

  for (int i = 0; i < mockObject.size(); i++) {
   System.out.println("I am priting mockObject.get(i):"
     + mockObject.get(i));

  // Here we are mocking an Iterator
  Iterator i = mock(Iterator.class);
  String result = + " " +;
  // This makes test successful
  assertEquals("Hello World", result);
 // This makes test fail
  // assertEquals("Hello World1", result);

 Maven Dependencies for Mockito and JUnit

  <!—Or if you needs extra dependencies: objenesis & hamcrest -->

<project xmlns="" xmlns:xsi=""

No comments:

Post a Comment