Mokito Framework

December 2016

In computer programming for testing Java applications, the framework Mokito is used. This is an open source / free framework that is available for download. It is essentially a testing framework that helps in error detection. The implementation of Mokito as a mocking frame work in creating ' Test Double ' objects, i.e. mock objects, ensures that the interaction with mocks remains right.


Intro


Mokito is a framework for testing Java applications. It simulates the behavior of complex objects and facilitates the development of unit testing and error detection.

Mockito is OpenSource and is available at the following address: http://mockito.org/.


Mockito is based on the principle of a mock object that simulates the behavior of another object.

The implementation of Mockito testing is done in several steps:
  • Creation of the mock object
  • Description of expected behavior
  • Using mocks
  • Checking that the interaction with mocks is correct

Example


public class ClassA {      
public ClassA()      
{      
}      
public void fonc1()      
{      
}      
public void fonc2()      
{      
}      
public int fonc3(int a, int b)      
{      
return a+b;      
}      
}      


public class ClassB {      

ClasseA ca ;      
public ClassA getCa()       
{      
return ca;      
}      

public void setCa(ClassA ca)       
{      
this.ca = ca;      
}      

public ClassB()      
{      
ca =new ClassA();      
}      

public void met1()      
{      
ca.fonc1();      
ca.fonc1();      
System.out.println(ca.fonc3(5, 6));      
}      
public void met2()      
{      
ca.fonc2();      
}      

}


Unit testing class ClassB:

import static org.mockito.Mockito.mock;      
import org.mockito.Mockito;      

public class Test       
{      

public static void main(String[] args)       
{      
ClassA ca = mock(ClassA.class);      
ClassB cb =new ClassB();      
cb.setCa(ca);      
cb.met1();      
Mockito.verify(ca, Mockito.times(2)).fonc1();      
Mockito.verify(ca, Mockito.times(1)).fonc3( Mockito.anyInt(), Mockito.anyInt());      
}      


}



Code Analysis:

ClassA ca = mock (ClassA.class);
=> It creates an AC, a mock class ClassA.

cb.setCa(ca);
=> It affects the ca object to the cb object.

cb.met1 ();
=> Calls the object with the cb met1() method.

Mockito.verify (ca Mockito.times(2)).func1 ();
=> Verifies that the method of mock ca func1 is called twice.

Mockito.verify (ca, Mockito.times(1)).fonc3( Mockito.anyInt(), Mockito.anyInt());
=> Verifies that the method of mock func3 ca is called only once by two integer parameters.

When the check fails an exception is generated.

Example:

Mockito.verify(ca, Mockito.atLeast(1)).fonc2();


=> This line generate the following exception:
Exception in thread "main" Wanted but not invoked:
classA.fonc2();      
-> at Test01.main(Test01.java:16)



==> It indicates that the method func2() mock the AC is not known.

You can also change the results returned from a function.
System.out.println(" The result of ca.fonc(3,3) is "+ca.fonc3(3, 3));      
Mockito.when(ca.fonc3( Mockito.anyInt(), Mockito.anyInt())).thenReturn(Integer.valueOf(5));      
System.out.println(" The result of ca.fonc(3,3) is "+ca.fonc3(3, 3));      



Code Analysis:
Mockito.when(ca.fonc3( Mockito.anyInt(), Mockito.anyInt())). thenReturn(Integer.valueOf(5));


# The return value of function fonc3 (mock) was 0.
We force it to 5.

This is especially useful when using functions that contains several features (conditions, loops etc.) and we want to verify the behavior of any function. We need therefore to configure mocks used in this function so that it passes all of these conditions.

A tip from wjaouadi.

Related :

This document entitled « Mokito Framework » from CCM (ccm.net) is made available under the Creative Commons license. You can copy, modify copies of this page, under the conditions stipulated by the license, as this note appears clearly.