:::: MENU ::::

Mockito


Mockito is a test framework for Java. It allows us to simulate objects without having to instanciate them completely. By doing this we can create tests of certain parts of the code without having to wait for the completion of the dependent components, because we can simulate them.

Installation

With Maven.

  	<dependency>
  		<groupId>org.mockito</groupId>
  		<artifactId>mockito-all</artifactId>
  		<version>2.0.2-beta</version>
  	</dependency>

Configuration

The tool is not configurable, but it is necessary to create a setup() method in the tests to initiate the simulated objects.

Basic example

We create a service (childService) and later another that depends of the first (parentService).

ChildService.java

package com.luisgomezcaballero.mockitodemo;

public class ChildService {

	public int check123(int number) {

		if (number == 123) {
			return 1;
		} else {
			return 0;
		}

	}

}

ParentService.java

package com.luisgomezcaballero.mockitodemo;

public class ParentService {

	private ChildService childService;

	public ChildService getChildService() {
		return childService;
	}

	public void setChildService(ChildService childService) {
		this.childService = childService;
	}

	public int checkCorrectNumber(int myNumber) {

		if (childService.check123(myNumber) == 1) {
			return 1;
		} else {
			return 0;
		}
	}
}

We create a test:

ParentServiceTest.java

package com.luisgomezcaballero.mockitodemo;

import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import org.junit.Before;
import org.junit.Test;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;

import com.luisgomezcaballero.mockitodemo.ChildService;
import com.luisgomezcaballero.mockitodemo.ParentService;

public class ParentServiceTest {

	@Mock
	ChildService childService;

	@Before
	public void setup() {
		MockitoAnnotations.initMocks(this);
	}

	@Test
	public void testCheckCorrectNumber() {
		ParentService parentService = new ParentService();
		parentService.setChildService(childService);
		int testNumber = 123;

		when(childService.check123(testNumber)).thenReturn(1);

		int checkCorrectNumber = parentService.checkCorrectNumber(testNumber);

		assertEquals(checkCorrectNumber, 1);
		verify(childService).check123(testNumber);
	}

}

The core of Mockito is the simulation that we do with:

when(childService.check123(testNumber)).thenReturn(1);

By doing this we will simulate the method of the childService and therefore we will be able to test the method of the parentService.

As we can see, later, we use any type of assert to confirm that the obtained result is the expected one.

Example with exceptions

It is the same, but we have to use

when().thenThrow(Exception.class)

And anotate the test method with:

@Test(expected = Exception.class)

Verification

Important: although we have a satisfactory test it is possible the method we are simulating does not execture (and by this the test will not be complete). To avoid these exceptions and to assure that the simulated method is correctly executed, we use after the assert:

verify(childService).check123(testNumber);

Repository

The code of this project can be found at https://github.com/luisgomezcaballero/mockito-demo.git.


So, what do you think ?