JUnit Mockito doNothing Example

Introduction

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.

Prerequisites

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

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">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.roytuts</groupId>
    <artifactId>junit-mockito-donothing</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>

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) {
		activityDao.createActivity(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;

@RunWith(PowerMockRunner.class)
public class JunitDoNothingTest {

	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.doNothing().when(dao).createActivity(activity);
	}

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

}

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

Source Code

Download

Thanks for reading.

Leave a Comment