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
http://code.google.com/p/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
http://code.google.com/p/mockito/downloads/list
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 {

 @Test
 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);
  mockedList.add("1");
  // This will make test result successful
  verify(mockedList).add("1");
  // This will make test result failure
  // verify(mockedList).add("2");

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

  // Here we are mocking an Iterator
  Iterator ii = mock(Iterator.class);
  ii.next();
  verify(ii).next();
  // 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);
  mockedList1.add("1");
  mockedList1.add("1");

  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);
  mockedList2.add("one");

  when(mockedList2.get(0)).thenReturn("first");
  System.out.println(mockedList2.get(0));

  when(mockedList2.add("1")).thenReturn(true);
  System.out.println(mockedList2.add("1"));

  when(mockedList2.add("1")).thenReturn(false);
  System.out.println(mockedList2.add("1"));

  System.out.println(mockedList2.size()); // return 0
  when(mockedList2.size()).thenReturn(100);
  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
  when(mockObject.size()).thenReturn(10);
  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);
  when(i.next()).thenReturn("Hello").thenReturn("World");
  String result = i.next() + " " + i.next();
  // This makes test successful
  assertEquals("Hello World", result);
 // This makes test fail
  // assertEquals("Hello World1", result);
}
}

 Maven Dependencies for Mockito and JUnit
<dependency>
  <groupId>org.mockito</groupId>
  <artifactId>mockito-core</artifactId>
  <version>1.9.0-rc1</version>
  <scope>test</scope>
</dependency>

  <!—Or if you needs extra dependencies: objenesis & hamcrest -->
<dependency>
    <groupid>org.mockito</groupid>
    <artifactid>mockito-all</artifactid>
    <version>1.8.5</version>
    <scope>test</scope>
</dependency>

Maven POM.XML
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="
http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>MockitoTest</groupId>
  <artifactId>MockitoTest</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <name>MockitoTest</name>
  <url>http://maven.apache.org</url>
<dependencies>
  <dependency>
   <groupId>org.mockito</groupId>
      <artifactId>mockito-core</artifactId>
      <version>1.8.5</version>
      <scope>test</scope>
  </dependency>
 <dependency>
   <groupId>junit</groupId>
   <artifactId>junit</artifactId>
   <version>4.8.2</version>
   <scope>test</scope>
  </dependency>
</dependencies>
<build>
  <finalName>MockitoTest</finalName>
  <testSourceDirectory>src</testSourceDirectory>
  <testResources>
   <testResource>
    <directory>src</directory>
   </testResource>
  </testResources>
 <plugins>
   <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>2.1</version>
    <configuration>
     <source>1.6</source>
     <target>1.6</target>
    </configuration>
   </plugin>
   <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-site-plugin</artifactId>
    <version>2.3</version>
    <configuration>
     <locales>en</locales>
    </configuration>
   </plugin>
   </plugins>
 </build>
</project>

No comments:

Post a Comment