JUnit 5 parameterized feature

Juan Aranda
Globant

--

Introduction

JUnit5 is the next-generation test framework, a very common tool for developers to write unit test classes. This article will teach you how to use a new feature named Parameterized. Let’s start with an overview, and afterward, we’ll following for how to configure and how to use this feature.

JUnit5 Overview

JUnit5 is the new version of this framework, composed of the following components:

  • JUnit Platform, the testing framework on JVM.
  • JUnit Jupiter, a new extension for writing test classes.
  • JUnit Vintage, test-engine for running Junit 3 and Junit 4.

Configuring parameterized

Before starting to use the Junit parameterized feature, we need to add the dependency in our pom.xml file:

For projects where we are using maven :

For projects where we are using Gradle :

NOTE : Version 5.7.0 is the lastest available at the time of this article.

Using parameterized features

Let’s use the EmailValidator class in order to show different uses cases for JUnit parameterized. This class is very simple but should be enough to showcase its functionality.

  • Test without parameterized feature, we can write a simple test class implementing the following test cases:

If you see the above test class we need to create two different methods in order to test both situations, checking valid and invalid e-mail addresses. But if we use the parameterized feature the above code could be changed to:

We can see two methods, that allow us to check multiple valid e-mail addresses and another for checking multiple invalid e-mail addresses, all the input values that we need are clearer for both methods, but we still have two methods, so, let’s see another option that becomes available to us using the parameterized feature:

In this case, we are using a method as a source for all the needed data to test (input values) both valid and invalid e-mail addresses. With this option, we are reducing the code in our test class. However we have yet another option, let’s take a look at the following code:

We can see the annotation @CsvFileSource. This allow us to define a CSV file with all the input data needed in our test, let’s look at this file:

This file has the expected validation for each input data, in this case, valid and invalid e-mail addresses. We can see excellent options to test our class, but we still need to perform additional testing, namely the possibility to have empty and null input values. Let’s see how we can test those situations.

  • Test null input values, in this case, the correct feature to use is @NullSource, this annotation allows us to catch null values in our test classes, let’s see the unit test method implementing this annotation:

Simple, right? with this feature our tests are clearer and concise.

  • Test empty input values, in this case, the correct feature to use is @EmptySource, this annotation allows us to catch empty values in our test classes, let’s see the uni test method implementing this annotation.

Excellent!, however, we now have two methods to test both situations, null and empty values, but the framework comes to us with another useful feature called @NullAndEmptySource let’s see the unit test method implementing this annotation:

As we can see this annotation reduces our code even further. However, we can combine features to create test cases that are even smaller than the above and perform even more tests, let’s take a look at:

In this case, we are combining tests for null, empty values, tabs, and newlines, all possible input values that we need to test in our EmailValidator class.

Something else we need to do is catch the exception when our EmailValidator class throws one if it receives incorrect input values, let’s see the options to test this situation.

  • Using the Try-catch clause, using this clause is a good approach because we achieved the goal, this is catching the exception let’s see the code:

The validation is correct, but let’s see the feature that we have available by using JUnit 5.

With this feature, the test is cleaner and much more simple, right?

Conclusion

We just went through some new features using Junit 5 that provides us developers some very nice options to create our unit tests and also to avoid boilerplate code that increases the class size, hopefully, this overview gives you a quick idea of what these features can do, and if you find them useful, make sure to use them in your next project.

--

--

Juan Aranda
Globant
Writer for

System engineer, focused on Software developer, he has worked on interesting projects for top companies, he likes to learn about how to build quality software.