Stubbing a non-static void method to do something

In this post under Mockito, I will show with example how to stub a void method to execute code other than real code.

For our example, I will use the below classes

MyCustomLogger

package package8;
public class MyCustomLogger {
public void info() {
System.out.println("adding a and b");
}
}

Calculator

package package8;
public class Calculator {
private MyCustomLogger myCustomLogger;
public int add(int a, int b){
myCustomLogger.info();
return a + b;
}
}

As you can see in the above code, the “Calculator” class is depender and “MyCustomLogger” class is dependee.

We need to test the “add” method of “Calculator” class and so we have to mock “MyCustomLogger” class.

Currently the “MyCustomLogger” class is printing the message to console.

As part of this example, we will create a mock of “MyCustomLogger” and create a stub of its void method “info” to display “Hello World” instead of printing “adding a and b”.

For this we will take help of Mockito’s “Answer” interface.

Mockito’s “Answer” interface lets you define the stubbed code that has to be executed instead of real code.

Once we come up with an implementation of “Answer” interface, we can tell Mockito to execute this instead of real code by calling Mockito’s static “doAnswer” method and passing the implementation of “Answer” interface.
interface as an argument.

This “Answer” interface has only one method “answer” which takes an instance of “InvocationOnMock” class. This instance is provided by Mockito at runtime.

Using the instance of “InvocationOnMock” class, we can at runtime
1) get the arguments passed to stubbed method
2) call the real method of the mocked object
3) get access to stubbed method object
4) get access to mock object

Below is the test class showing how to do it

CalculatorTest

package package8;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.junit.jupiter.MockitoExtension;
import org.mockito.stubbing.Answer;
import static org.mockito.ArgumentMatchers.anyString;
@ExtendWith(MockitoExtension.class)
public class CalculatorTest {
@InjectMocks
private Calculator calculator;
@Mock
private MyCustomLogger myCustomLogger;
@Test
public void testAdd() {
Mockito.doAnswer(new Answer<Void>() {
@Override
public Void answer(InvocationOnMock invocationOnMock) throws Throwable {
System.out.println("Hello World");
return null;
}
}).when(myCustomLogger).info();
calculator.add(10, 5);
}
}

In the method “testAdd”, I provide an anonymous implementation of “Answer” interface, which returns Void but prints “Hello World” to the console.

At the same time we are passing this anonymous implementation as an argument to “doAnswer” method of Mockito.

So finally at line 24,we are telling Mockito that when “myCustomLogger” void method “info” is called run the code present in anonymous “Answer” implementation instead of real method.

In this way, we can stub a non-static void method of a mock object to execute code other than real code.

Leave a comment