JUnit Mockito doNothing Example


In this post, I will be showing Junit mockito doNothing example in one of the most widely used and popular JUnit Testing Mocking framework – Mockito. I am going to use PowerMock API to test Java classes. I am going to show you when to use doNothing() method.


Java at least 8, Maven 3.6.3, Gradle 6.5.1, Junit 4.11, PowerMock 1.5.6

Project Setup

You can create gradle or maven based project in your favorite IDE or tool.

For gradle based project you can use below build.gradle script file:

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

If you are creating maven based project then modify the pom.xml file as shown below.

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

Model Class

Create a below model class that will be used finally in DAO layer to query database or persistence layer.

package com.roytuts.junit.mockito.donothing;

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 and DAO

Create service class though this class does not do any real business activity.

package com.roytuts.junit.mockito.donothing;

public class ActivityService {

	private ActivityDao activityDao;

	public ActivityService() {
		activityDao = new ActivityDao();

	public void createActivity(final ActivityModel activity) {


Create dao class with the below source code and this class also does not do any real activity.

package com.roytuts.junit.mockito.donothing;

public class ActivityDao {

	public void createActivity(final ActivityModel activity) {
		// TODO : create new Activity

Junit Class

Create Junit class to test our service class. Here I will see how to use doNothing() method from Mockito framework.

I am using doNothing() method on void method only because void method does not return anything.

If you want to verify whether your void method was tested or executed in test case then you can check tutorial on Mockito verify() example.

package com.roytuts.junit.mockito.donothing;

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;

public class JunitDoNothingTest {

	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

	private ActivityModel getActivityModel() {
		return new ActivityModel("1000", "Activity_TODO");


The above test case will pass when run as Junit test case.

Source Code


Thanks for reading.

Leave a Reply

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