Contents

Mockito mock void method

Writen by: David Vlijmincx

Introduction

In this post, we will look at how to mock void methods with Mockito. Methods that return void can't be used with when. This means we have to work with the following methods to mock a void method:

  • doThrow(Throwable...)
  • doThrow(Class)
  • doAnswer(Answer)
  • doNothing()
  • doCallRealMethod()

This is the class we will be using for the examples. It's a small class with only one void method that prints “Hello, " followed by the input to the console.

1
2
3
4
5
class SomeClass{
    void doAction(String name){
        System.out.println("Hello, " + name);
    }
}

Throwing an exception with a mocked void method

To make a void method throw an exception, we use doThrow(). The exception we pass to the doThrow() is thrown when the mocked method is called.

1
2
3
SomeClass mockInstance = mock(SomeClass.class);
Mockito.doThrow(new RuntimeException()).when(mockInstance).doAction(anyString());
mockInstance.doAction("World!");

You can only use unchecked exceptions or exceptions that are declared in the method signature. The doThrow() call below wouldn't work because IOException is a checked exception and not declared as part of the doAction() method signature.

1
Mockito.doThrow(new IOException()).when(mockInstance).doAction(anyString());

Verifying that a void method is called

With Mockito, we use verify() to check if a method has been called. The example below shows two ways of implementing this. times(1) is the default value, so you can omit it to have more readable code.

1
2
3
4
5
6
7
SomeClass mockInstance = mock(SomeClass.class);
mockInstance.doAction("World!");

// using the default
Mockito.verify(mockInstance).doAction(anyString());
// explicitly stating how many times a method is called
Mockito.verify(mockInstance, times(1)).doAction(anyString());

Capturing void method arguments

We can also capture the arguments that we pass to a method. To do that we can create an ArgumentCaptor or use lambda matchers. Line 4 till 6 shows how to create and use the ArgumentCaptor. Lines 9 uses a lambda matcher. At line 11, we also use a lambda matcher, but with a method reference to keep it more readable.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
SomeClass mockInstance = mock(SomeClass.class);
mockInstance.doAction("World!");

ArgumentCaptor<String> captor = ArgumentCaptor.forClass(String.class);
Mockito.verify(mockInstance).doAction(captor.capture());
assertEquals("World!", captor.getValue());

// using lambda matchers
verify(mockInstance).doAction(argThat(i -> "World!".equals(i)));
// using lambda matchers with method reference
verify(mockInstance).doAction(argThat("World!"::equals));

Stub a void method to run other code

We can also stub a method to let it perform other behavior. We use Mockito's doAnswer() to change the behavior. The example below changed the message that gets printed to the console. The method will now print good morning instead of hello.

1
2
3
4
5
6
7
8
SomeClass mockInstance = mock(SomeClass.class);

doAnswer(invocation -> {
    System.out.println("Good morning, " + invocation.getArgument(0));
    return null;
}).when(mockInstance).doAction(anyString());

mockInstance.doAction("World!");

Calling the real method

By default, stubbed will do nothing when you call them. To call the actual method, you can use doCallRealMethod(). Doing this will let you call the actual method of the class you mock.

1
2
3
4
5
SomeClass mockInstance = mock(SomeClass.class);

doCallRealMethod().when(mockInstance).doAction(anyString());

mockInstance.doAction("World!");

Conclusion

In this post, we looked at the different ways of mocking void methods when you write tests using Mockito. You learned how to mock void methods with Mockito using different methods.

 




Questions, comments, concerns?

Have a question or comment about the content? Feel free to reach out!