JUnit Mockito doReturn Example

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.


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 {

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">

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();
		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;

public class JunitDoReturnTest {

	private ActivityService service;
	private ActivityDao dao;
	private ActivityModel activity;

	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);

	public void tearDown() throws Exception {

	public void testGetActivity() {
		// return mock activity model object instead of real object from dao
		// 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.

Leave a Reply

Your email address will not be published. Required fields are marked *