Java Junit 5 Mockito doNothing() Example

In this tutorial I am going to show you how to work with Mockito‘s doNothing() in Java Junit 5 API. Mockito‘s doNothing() or Mockito itself is widely used testing framework. If you are working with Junit 4 then you can read the similar example Mockito’s doNothing() using Junit 4.

Mockito‘s doNothing() is used when you want to test void methods because void methods do not return anything so there is no way you can verify using assert. These void methods may be anywhere, for example, in service class, in dao class, etc.

Prerequisites

Eclipse 2020-06, Java at least 1.8, Junit 5, Gradle 6.5.1

How to configure Junit 5 for Java projects

Project Setup

Create gradle based project in Eclipse. The name of the project is java-junit-5-mockito-donothing.

Update the build.gradle script with the required dependencies as shown below:

plugins {
    id 'java-library'
}

repositories {	
    jcenter()
}

dependencies {
    testImplementation 'org.junit.jupiter:junit-jupiter-engine:5.7.0-M1'
	testImplementation 'org.junit.platform:junit-platform-engine:1.7.0-M1'
	testImplementation 'org.mockito:mockito-junit-jupiter:3.4.4'
}

POJO Class

This is just a simple class that holds object’s state.

package com.roytuts.java.junit.mockito.donothing.model;

public class ActivityModel {

	private String id;
	private String name;

	public ActivityModel() {
	}

	public ActivityModel(String id, String name) {
		this.id = id;
		this.name = name;
	}

	//getters and setters

}

DAO Class

A simple dao class that does some database activities but here I am doing nothing for this example.

package com.roytuts.java.junit.mockito.donothing.dao;

import com.roytuts.java.junit.mockito.donothing.model.ActivityModel;

public class ActivityDao {

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

}

Service Class

The service class that does the business logic. But for this example I have kept it simple.

package com.roytuts.java.junit.mockito.donothing.service;

import com.roytuts.java.junit.mockito.donothing.dao.ActivityDao;
import com.roytuts.java.junit.mockito.donothing.model.ActivityModel;

public class ActivityService {

	private ActivityDao activityDao;

	public ActivityService(ActivityDao activityDao) {
		this.activityDao = activityDao;
	}

	public void createActivity(final ActivityModel activity) {
		activityDao.createActivity(activity);
	}

}

Junit Class

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

We are using doNothing() method on void method only because void method does not return anything.

I am using @ExtendWith(...) instead of @RunWith(...) in Junit 5 test class.

Using Junit 5 how it is easy to use Mockito framework. You just need to use @Mock annotation to mock the object.

I am testing doNothing() both on dao and service class’ methods.

package com.roytuts.java.junit.mockito.donothing.service;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.junit.jupiter.MockitoExtension;

import com.roytuts.java.junit.mockito.donothing.dao.ActivityDao;
import com.roytuts.java.junit.mockito.donothing.model.ActivityModel;

@ExtendWith(MockitoExtension.class)
public class ActivityServiceTest {

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

	@Test
	public void testDaoCreateActivity() {
		Mockito.lenient().doNothing().when(dao).createActivity(activity);
	}

	@Test
	public void testServiceCreateActivity() {
		Mockito.lenient().doNothing().when(service).createActivity(activity);
	}

}

Testing doNothing()

Running the Junit class will give you the success results as shown below in the image:

java junit 5 mockito doNothing example

That’s all. Hope you got an idea how to use doNothing() method on void methods for testing using Junit 5.

Source Code

Download

Thanks for reading.

Leave a Comment