DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Because the DevOps movement has redefined engineering responsibilities, SREs now have to become stewards of observability strategy.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

Related

  • How to Unit Test Classes Which Create New Objects
  • Testing Asynchronous Operations in Spring With JUnit 5 and Byteman
  • Mastering Unit Testing and Test-Driven Development in Java
  • Hints for Unit Testing With AssertJ

Trending

  • Designing Fault-Tolerant Messaging Workflows Using State Machine Architecture
  • ITBench, Part 1: Next-Gen Benchmarking for IT Automation Evaluation
  • Data Lake vs. Warehouse vs. Lakehouse vs. Mart: Choosing the Right Architecture for Your Business
  • How Kubernetes Cluster Sizing Affects Performance and Cost Efficiency in Cloud Deployments
  1. DZone
  2. Testing, Deployment, and Maintenance
  3. Testing, Tools, and Frameworks
  4. Unit Testing Void Methods with Mockito and JUnit

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.

By 
Upanshu Chaudhary user avatar
Upanshu Chaudhary
·
Dec. 11, 20 · Code Snippet
Likes (1)
Comment
Save
Tweet
Share
204.7K Views

Join the DZone community and get the full member experience.

Join For Free

Unit testing with mockito

Writing 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: Informationand Publishing.

TheInformationclass looks like this:

Java
 




xxxxxxxxxx
1
12


 
1
public class Information {
2

          
3
   private final Publishing publishing;
4

          
5
   public Information(Publishing publishing) {
6
       this.publishing = publishing;
7
   }
8

          
9
   public void sendInfoForPublishing(Person person) {
10
       publishing.publishInformation(person);
11
   }
12
}



As we can see, the methodsendInformationForPublishing()is a void method. The logic for the method is simple. It takes a Personobject as a parameter and passes the object to the method of thePublishingclass.

The method publishInformation()is also a void method.

Java
 




xxxxxxxxxx
1


 
1
public class Publishing {
2

          
3
   public void publishInformation(Person person) {
4
       System.out.println(person);
5
   }
6
}



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.
Java
 




xxxxxxxxxx
1


 
1
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:

Java
 




xxxxxxxxxx
1
16


 
1
public class InformationTest {
2
3
   Publishing publishing = mock(Publishing.class);
4
5
   @Autowired
6
   private Information information;
7
8
   @Test
9
   void whenSendInformationForPublishingIsSuccessful() {
10
       information = new Information(publishing);
11
       Person person = ObjectCreator.getPerson();
12
       doNothing().when(publishing).publishInformation(person);
13
       information.sendInfoForPublishing(person);
14
       verify(publishing,times(1)).publishInformation(person);
15
   }
16
}


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.

test passed example

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.

test fail example

That’s it! It’s that simple. This is how we can unit test the void methods. 

The GitHub link for this implementation is provided here. Simply clone it; have fun!

unit test Mockito Test case JUnit Java (programming language)

Published at DZone with permission of Upanshu Chaudhary. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • How to Unit Test Classes Which Create New Objects
  • Testing Asynchronous Operations in Spring With JUnit 5 and Byteman
  • Mastering Unit Testing and Test-Driven Development in Java
  • Hints for Unit Testing With AssertJ

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!