Mockito Mock Void Method

Filed Under: Mockito

Most of the times Mockito when() method is good enough to mock an object’s behavior. But when we have to mock a void method, we can’t use when().

Mockito Mock Void Method

Mockito provides following methods that can be used to mock void methods.

  1. doAnswer(): We can use this to perform some operations when a mocked object method is called that is returning void.
  2. doThrow(): We can use doThrow() when we want to stub a void method that throws exception.

Let’s create a simple class with a void method that we will mock in our test classes.

package com.journaldev;

public class Employee {

	private String name;

	public String getName() {
		return name;

	public void setName(String name) {
		if (name == null)
			throw new IllegalArgumentException("Employee Name can't be null"); = name;


Mockito mock void method example

Mockito doAnswer() method takes Answer as argument. It’s a functional interface so we can use lambda expression for its implementation.

doAnswer((i) -> {
	System.out.println("Employee setName Argument = " + i.getArgument(0));
	return null;

Notice that return null statement is required since we are mocking void method.

Mockito mock void method with exception

Below code snippet shows how to use doThrow() method to mock void methods with the exception.


JUnit Mockito mock void method example

Here is a complete example in JUnit where I am using Mockito to mock void method.

package com.journaldev.mockito.voidmethod;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.doAnswer;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

import org.junit.jupiter.api.Test;

import com.journaldev.Employee;

class JUnitMockitoVoidMethod {

	void test_mockito_void() {
		Employee emp = mock(Employee.class);


		doAnswer((i) -> {
			System.out.println("Employee setName Argument = " + i.getArgument(0));
			return null;


		assertThrows(IllegalArgumentException.class, () -> emp.setName(null));

		assertEquals("Pankaj", emp.getName());


TestNG Mockito void method example

Since JUnit 5 and TestNG annotations are so similar, we don’t have to any code specific changes in above class to switch from JUnit 5 to TestNG.

Just remove the JUnit 5 import statements and add below imports to change testing framework from JUnit to TestNG.

import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertThrows;
import static org.testng.Assert.assertTrue;

import org.testng.annotations.Test;
You can download the complete project code from our GitHub Repository.


  1. shyam says:

    I want to mock a void method whose one input state got changed in method body

    public void doSomething(SomeBusinessClass1 input1,SomeBusinessClass2 input2,SomeBusinessClass3 input3)
    if (openartion on input1){
    How can do this ?

Comments are closed.

Generic selectors
Exact matches only
Search in title
Search in content