Mockito

Mockito ThenReturn Example

In this example we will learn how to use ‘thenReturn’ method of Mockito. A unit test should test a class in isolation. Side effects from other classes or the system should be eliminated if possible. Mockito lets you write beautiful tests with a clean & simple API. Tools and technologies used in this example are Java 1.8, Eclipse Luna 4.4.2

1. Introduction

Mockito is a popular mocking framework which can be used in conjunction with JUnit. Mockito allows us to create and configure mock objects. Using Mockito simplifies the development of tests for classes with external dependencies significantly. We can create the mock objects manually or can use the mocking framewors like Mockito, EasyMock. jMock etc. Mock frameworks allow us to create mock objects at runtime and define their behavior. The classical example for a mock object is a data provider. In production a real database is used, but for testing a mock object simulates the database and ensures that the test conditions are always the same.

2. Creating a project

Below are the steps we need to take to create the project.

  1. Open Eclipse. Go to File=>New=>Java Project. In the ‘Project name’ enter ‘MockitoThenReturnExample’.
  2. Eclipse will create a ‘src’ folder. Right click on the ‘src’ folder and choose New=>Package. In the ‘Name’ text-box enter ‘com.javacodegeeks’. Click ‘Finish’.
  3. Right click on the package and choose New=>Class. Give the class name as ThenReturnExampleTest. Click ‘Finish’. Eclipse will create a default class with the given name.

2.1 Dependencies

For this example we need the junit and mockito jars. These jars can be downloaded from Maven repository. We are using ‘junit-4.12.jar’ and ‘mockito-all-1.10.19.jar’. There are the latests (non-beta) versions available as per now. To add these jars in the classpath right click on the project and choose Build Path=>Configure Build Path. The click on the ‘Add External JARs’ button on the right hand side. Then go to the location where you have downloaded these jars. Then click ok.

Figure 1. Add External JAR
Figure 1. Add External JAR

3. thenReturn

The thenReturn() methods lets you define the return value when a particular method of the mocked object is been called. The below snippet shows how we use thenReturn to check for multiple values.

Iterator i = mock(Iterator.class);
when(i.next()).thenReturn("Java Code Geeks").thenReturn("Mockito");
String result = i.next() + " " + i.next();
System.out.println(result);

The first time next() method is called ‘Java Code Geeks’ is returned and when it’s called the second time ‘Mockito’ is returned. So the result is Java Code Geeks Mockito.

The below code snippet shows how to return values based on input parameter.

Comparable c= mock(Comparable.class);
when(c.compareTo("Java Code Geeks")).thenReturn(100);
when(c.compareTo("Mockito")).thenReturn(200);
assertEquals(200,c.compareTo("Mockito"));

The code snippet below shows how you can return the same value independent of the value of the parameter passed.

Comparable c = mock(Comparable.class);
when(c.compareTo(anyInt())).thenReturn(0);
assertEquals(0 ,c.compareTo(9));

4. Code

Below is the test class we will use to show the usage of ‘thenReturn()’. This class can be run as a JUnit test from eclipse.

ThenReturnExampleTest.java

package com.javacodegeeks;

import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import static org.mockito.Matchers.anyInt;

import java.util.Iterator;

import org.junit.Test;

@SuppressWarnings({"rawtypes", "unchecked"})
public class ThenReturnExampleTest {

/**
* This will test multiple return values.
* @throws Exception
*/
@Test
public void test1() throws Exception {
Iterator i = mock(Iterator.class);
when(i.next()).thenReturn("Java Code Geeks").thenReturn("Mockito");
String result = i.next() + " " + i.next();
assertEquals("Java Code Geeks Mockito", result);
}

/**
* This test demonstrates how to return values based on the input
*/
@Test
public void test2() {
Comparable c= mock(Comparable.class);
when(c.compareTo("Java Code Geeks")).thenReturn(100);
when(c.compareTo("Mockito")).thenReturn(200);
assertEquals(200,c.compareTo("Mockito"));
}

/**
* This test demonstrates how to return values independent of the input value
*/
@Test
public void test3() {
Comparable c = mock(Comparable.class);
when(c.compareTo(anyInt())).thenReturn(0);
assertEquals(0 ,c.compareTo(9));
}
}

5. Download the source file

This was an example of Mockito thenReturn().

Download
You can download the full source code of this example here Mockito thenReturn Example.

Mohammad Meraj Zia

Senior Java Developer
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Inline Feedbacks
View all comments
Back to top button