In this post, I will be showing one of the most widely used and popular JUnit Testing Mocking framework – Mockito. Mockito is one of the widely used testing API for Java. I am going to show you how to work with doReturn()
and when to use it for testing your Java class using Junit.
Prerequisites
Java at least 8, Gradle 6.5.1, Maven 3.6.3, Junit 4.11, Power Mock 1.5.6
Project Setup
You can create either gradle or maven based project in your favorite IDE or tool.
For gradle based project you can use below build.gradle script:
plugins {
id 'java-library'
}
repositories {
jcenter()
}
sourceCompatibility = 12
targetCompatibility = 12
dependencies {
testImplementation 'junit:junit:4.11'
testImplementation ('org.powermock:powermock-api-mockito:1.5.6')
testImplementation ('org.powermock:powermock-module-junit4:1.5.6')
}
For maven based project you can use below pom.xml file:
<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>com.roytuts</groupId>
<artifactId>junit-mockito-doreturn</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<jdk.version>1.8</jdk.version>
<junit.version>4.11</junit.version>
<powermock.version>1.5.6</powermock.version>
</properties>
<dependencies>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-module-junit4</artifactId>
<version>${powermock.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-api-mockito</artifactId>
<version>${powermock.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>${jdk.version}</source>
<target>${jdk.version}</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
Classes to be tested
For this example I am going to create some classes for model, service and DAO layer.
Model Class
package com.roytuts.junit.mockito.doreturn;
public class ActivityModel {
private String id;
private String name;
public ActivityModel() {
}
public ActivityModel(String id, String name) {
this.id = id;
this.name = name;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
Service Class
package com.roytuts.junit.mockito.doreturn;
import java.util.UUID;
public class ActivityService {
private ActivityDao activityDao;
public ActivityService() {
activityDao = new ActivityDao();
}
public ActivityModel getActivity(String id) {
ActivityModel activity = activityDao.getActivity(id);
final String newName = getActivityGeneratedName();
activity = updateActivityName(activity, newName);
return activity;
}
private String getActivityGeneratedName() {
String newName = "Activity_" + UUID.randomUUID().toString();
return newName;
}
private ActivityModel updateActivityName(ActivityModel activity, String name) {
ActivityModel activityModel = new ActivityModel();
activityModel.setId(activity.getId());
activityModel.setName(name);
return activityModel;
}
}
DAO Class
package com.roytuts.junit.mockito.doreturn;
public class ActivityDao {
public ActivityModel getActivity(String id) {
// TODO implement logic
System.out.println("New Activity fetched successfully");
return null;
}
}
Junit Class
I am writing below Junit class to test the above classes.
package junit.mockito.doreturn;
import static org.junit.Assert.assertEquals;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mockito;
import org.powermock.modules.junit4.PowerMockRunner;
import org.powermock.reflect.Whitebox;
import com.roytuts.junit.mockito.doreturn.ActivityDao;
import com.roytuts.junit.mockito.doreturn.ActivityModel;
import com.roytuts.junit.mockito.doreturn.ActivityService;
@RunWith(PowerMockRunner.class)
public class JunitDoReturnTest {
private ActivityService service;
private ActivityDao dao;
private ActivityModel activity;
@Before
public void setUp() throws Exception {
// below object should be real in order to carry out testing on it
service = new ActivityService();
// create mock object of DAO instead of using the real object
dao = Mockito.mock(ActivityDao.class);
// create new ActivityModel
activity = getActivityModel();
// set mock dao object to activityDao in service implementation class
Whitebox.setInternalState(service, "activityDao", dao);
}
@After
public void tearDown() throws Exception {
}
@Test
public void testGetActivity() {
// return mock activity model object instead of real object from dao
Mockito.doReturn(activity).when(dao).getActivity("0001");
// call the real service class method
ActivityModel activityModel = service.getActivity("0001");
// verify with assert
assertEquals(activityModel.getId(), activity.getId());
}
private ActivityModel getActivityModel() {
return new ActivityModel("1000", "Activity_TODO");
}
}
The above test class will be passed when you run as Junit test case.
Source Code
Thanks for reading.