Latest update on February 13, 2012 at 05:38 PM by Paul Berentzen .

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 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: https://siteockitorg/

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) { thia = ca; } public ClassB() { ca =new ClassA(); } public void met1() { caonc1(); caonc1(); Systemut.println(caonc3(5, 6)); } public void met2() { caonc2(); } }

Unit testing class ClassB:

import static orgockito.Mockitoock; import orgockito.Mockito; public class Test { public static void main(String[] args) { ClassA ca = mock(ClassAlass); ClassB cb =new ClassB(); cb.setCa(ca); cbet1(); Mockito.verify(ca, Mockitoimes(2onc1(); Mockito.verify(ca, Mockitoimes(1onc3( Mockito.anyInt(), Mockito.anyInt()); } }

Code Analysis:

ClassA ca = mock (ClassAlass);

=> It creates an AC, a mock class ClassA.

cb.setCa(ca);

=> It affects the ca object to the cb object.

cbet1 ();

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

Mockito.verify (ca Mockitoimes(2unc1 ();

=> Verifies that the method of mock ca func1 is called twice.

Mockito.verify (ca, Mockitoimes(1onc3( 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(1onc2();

=> This line generate the following exception:

Exception in thread "main" Wanted but not invoked:

classAonc2(); -> at Test01ain(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.

Systemut.println(" The result of caonc(3,3) is "+caonc3(3, 3)); Mockitohen(caonc3( Mockito.anyInt(), Mockito.anyInhenReturn(Integer.valueOf(5)); Systemut.println(" The result of caonc(3,3) is "+caonc3(3, 3));

Code Analysis:

Mockitohen(caonc3( 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.

Tags:
Tomasz David
Tomasz David

Leave a Comment