Unit Testing Void Methods with Mockito and JUnit
This blog is a quick and simple guide to understanding how we can test void methods in Java with JUnit and Mockito and how it makes testing easier for us.
Join the DZone community and get the full member experience.
Join For FreeWriting functionality is the main focus whenever we are writing a software program, but it is equally important that we make sure our code works the way we intended it to. And how do we do that? By writing unit test cases. They are used to test the smallest functionality of code. Unit test cases are an essential part of software development. In this blog, we are going to cover one of the testing scenarios and unit test cases for void methods.
How to Test Void Methods
As we already know that our aim is to test void methods in a class. But it is also really important to understand why we test void methods.
Whenever we write unit test cases for any method, we expect a return value from the method. Generally, we use assert for checking if the method returns the value that we expect it to return, but in the case of void methods, they do not return any value. So how do we check if our method is functioning properly? Let’s see using an example:
In this example, we are creating two classes: Information
and Publishing
.
TheInformation
class looks like this:
xxxxxxxxxx
public class Information {
private final Publishing publishing;
public Information(Publishing publishing) {
this.publishing = publishing;
}
public void sendInfoForPublishing(Person person) {
publishing.publishInformation(person);
}
}
As we can see, the methodsendInformationForPublishing()
is a void method. The logic for the method is simple. It takes a Person
object as a parameter and passes the object to the method of thePublishing
class.
The method publishInformation()
is also a void method.
xxxxxxxxxx
public class Publishing {
public void publishInformation(Person person) {
System.out.println(person);
}
}
Let us now see how we can write unit test cases for this simple implementation.
Using the verify()
Method
Whenever we mock a void method, we do not expect a return value. That is why we can only verify whether that method is being called or not.
Features of verify()
:
- Mockito provides us with a
verify()
method that lets us verify whether the mock void method is being called or not. - It lets us check the number of methods invocations. So, if the method invocation returns to be zero, we would know that our mock method is not being called.
xxxxxxxxxx
verify(publishing,times(1)).publishInformation(person);
The verify method takes two arguments. The mock method object and the number of invocations you want to verify. The expected number of invocations is passed in the times()
method. Let’s see how the test case will look:
xxxxxxxxxx
public class InformationTest {
Publishing publishing = mock(Publishing.class);
private Information information;
void whenSendInformationForPublishingIsSuccessful() {
information = new Information(publishing);
Person person = ObjectCreator.getPerson();
doNothing().when(publishing).publishInformation(person);
information.sendInfoForPublishing(person);
verify(publishing,times(1)).publishInformation(person);
}
}
As our function will callpublishInformation()
only once, so we have passed the value 1 in the times()
function. We know that when our test case will call the mocked publishInformation()
method, it will not do anything. We need to let Mockito know of this behavior. For this, we use thedoNothing()
method, which will, in simple terms, let Mockito know that it needs to do nothing when the given method is called.
If we change the number of invocations to any other value, the test case should fail.
Here I changed the number of invocations to 2.
The GitHub link for this implementation is provided here. Simply clone it; have fun!
Published at DZone with permission of Upanshu Chaudhary. See the original article here.
Opinions expressed by DZone contributors are their own.
Comments