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
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
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
Partner Zones AWS Cloud
by AWS Developer Relations
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
Partner Zones
AWS Cloud
by AWS Developer Relations
The Latest "Software Integration: The Intersection of APIs, Microservices, and Cloud-Based Systems" Trend Report
Get the report
  1. DZone
  2. Testing, Deployment, and Maintenance
  3. Testing, Tools, and Frameworks
  4. How to Expedite the Creation of JUnit Parameterized Tests

How to Expedite the Creation of JUnit Parameterized Tests

Let's take a look at three frameworks used to write JUnit tests, to define and run multiple test cases with parameterized tests.

Brian McGlauflin user avatar by
Brian McGlauflin
·
Sep. 22, 18 · Tutorial
Like (4)
Save
Tweet
Share
8.26K Views

Join the DZone community and get the full member experience.

Join For Free

When writing unit tests, it is common to initialize method input parameters and expected results in the test method itself. In some cases, using a small set of inputs is enough; however, there are cases in which we need to use a large set of values to verify all of the functionality in our code. Parameterized tests are a good way to define and run multiple test cases, where the only difference between them is the data. They can validate code behavior for a variety of values, including border cases. Parameterizing tests can increase code coverage and provide confidence that the code is working as expected.

There are a number of good parameterization frameworks for Java. In this article, we will look at three different frameworks commonly used with JUnit tests, with a comparison between them and examples of how the tests are structured for each. Finally, we will explore how to simplify and expedite the creation of parameterized tests.

Parameterized JUnit Test Frameworks

Let's compare the 3 most common frameworks: JUnit 4, JunitParams, and JUnit 5. Each JUnit parameterization framework has its own strengths and weaknesses.

JUnit 4

Pros:

  • This is the parameterization framework built into JUnit 4, so it requires no additional external dependencies.
  • It supports older versions of Java (JDK 7 and older).

Cons:

  • Test classes use fields and constructors to define parameters, which make tests more verbose.
  • It requires a separate test class for each method being tested.

JUnitParams

Pros:

  • Simplifies parameter syntax by allowing parameters to be passed directly to a test method.
  • Allows multiple test methods (each with their own data) per test class.
  • Supports CSV data sources, as well as annotation-based values (no method required).

Cons:

  • Requires the project to be configured with the JunitParams dependency.
  • When running and debugging tests, all tests within the class must be run - it is not possible to run a single test method within a test class.

JUnit 5

Pros:

  • This parameterization framework is built into JUnit 5 and improves what was included with JUnit 4.
  • Has a simplified parameter syntax like JunitParams.
  • Supports multiple data-set source types, including CSV and annotation (no method required).
  • Even though no extra dependencies are required, more than one .jar is needed.

Cons:

  • Requires Java 8 and newer version of build system (Gradle 4.6 or Maven Surefire 2.21).
  • May not be supported in your IDE yet (as of this writing, only Eclipse and IntelliJ support JUnit 5).

Example

As an example, suppose that we have a method that processes loan requests for a bank. We might write a unit test that specifies the loan request amount, down payment amount, and other values. We would then create assertions that validate the response - the loan may be approved or rejected, and the response may specify the terms of the loan.

For example:

First, let's look at a regular test for the above method:

In this example, we are testing our method by requesting a $1000 loan, with a $200 down payment and indicating that the requestor has $250 in available funds. The test then validates that the loan was approved and didn't provide a message in the response.

In order to make sure that our requestLoan() method is tested thoroughly, we need to test with a variety of down payments, requested loan amounts, and available funds. For instance, let's test a $1 million loan request with zero down payment, which should be rejected. We could simply duplicate the existing test with different values, but since the test logic would be the same, it is more efficient to parameterize the test instead.

We will parameterize the requested loan amount, down payment, and available funds, as well as the expected results: whether the loan was approved, and the message returned after validation. Each set of request data, along with its expected results, will become its own test case.

An Example Parameterized Test Using JUnit 4 Parameterized

Let's start with a Junit 4 Parameterized example. To create a parameterized test, we first need to define the variables for the test. We also need to include a constructor to initialize them:

Here, we see that the test uses the @RunWith annotation to specify that the test will run with the Junit4 Parameterized runner. This runner knows to look for a method which will provide the value-set for the test (annotated with @Parameters), initialize the test properly, and run the tests with multiple rows.

Note that each parameter is defined as a field in the test class, and the constructor initializes these values (you can also inject values into fields using the @Parameter annotation if you don't want to create a constructor). For each row in the value-set, the Parameterized runner will instantiate the test class and run each test in the class.

Let's add a method which provides the parameters to the Parameterized runner:

The value-sets are built as a List of Object arrays by the data() method, which is annotated with @Parameters. Note that @Parameters sets the name of the test using placeholders, which will be replaced when the test runs. This makes it easier to see values in test results, as we will see later. Currently, there is only one row of data, testing a case where the loan should be approved. We can add more rows to increase coverage of the method under test.

Here, we have one test case where the loan would be approved, and two cases in which it should not be approved for different reasons. We may want to add rows in which zero or negative values are used, as well as test boundary conditions.

We are now ready to create the test method:

Here, we reference the fields when invoking the requestLoan() method and validating the results.

JUnitParams Example

The JunitParams library simplifies parameterized test syntax by allowing parameters to be passed directly to the test method. The parameter values are provided by a separate method whose name is referenced in the @Parameters annotation.

JunitParams has the additional benefit that it supports using CSV files to provide values in addition to providing the values in code. This allows the test to be decoupled from the data and data values to be updated without updating the code.

JUnit 5 Example

JUnit 5 addresses some of the limitations and shortcomings of JUnit 4. Like JunitParams, Junit 5 also simplifies the syntax of parameterized tests. The most important changes in syntax are:

  • The test method is annotated with @ParameterizedTest instead of @Test
  • The test method accepts parameters directly, instead of using fields and a constructor
  • The @RunWith annotation is no longer needed

Defining the same example in Junit 5 would look like this:

Efficiently Create Parameterized Tests

As one might imagine, writing the above parameterized test can be a bit of work. For each parameterized test framework there is a bit of boilerplate code that needs to be written correctly. It can be hard to remember the correct structure, and it takes time to write out. To make this much easier, you can use Parasoft Jtest to generate parameterized tests, automatically, like the ones described above. To do this, simply select the method you want to generate a test for (in Eclipse or IntelliJ), and click the Parameterized action in Parasoft Jtest's Unit Test Assistant view:

The test is generated, using default values and assertions. You can then configure the test with real input values and assertions, and add more data rows to the data() method.

Running the Parameterized Test

Parasoft Jtest can run parameterized tests directly in both Eclipse and IntelliJ.

The JUnit View in Eclipse.

Note that the name of each test, as shown, includes input values from the dataset and expected result values. This can make debugging the test much easier when it fails since the input parameters and expected outputs are shown for each case.

You can also use the Run All action from Parasoft Jtest:

The Flow Tree view in Parasoft Jtest.

It analyzes the test flow and provides detailed information about the previous test run. This allows you to see what happened in the test without needing to rerun the test with breakpoints or debugging statements. For instance, you can see parameterized values in the Variables view:

The Variables View in Parasoft Jtest.

Conclusion

The three frameworks that we reviewed are fine choices and work well. If using JUnit 4, I prefer JunitParams over the built-in JUnit 4 Parameterized framework, due to the cleaner design of the test classes and the ability to define multiple test methods in the same class. However, if using JUnit 5, I'd recommend the built-in JUnit 5 framework since it addresses the shortcomings in JUnit 4 and requires no extra libraries. I also like using Parasoft Jtest's unit testing capabilities to make creation, execution, and debugging of parameterized tests more efficient.

unit test JUnit

Published at DZone with permission of Brian McGlauflin, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • How We Solved an OOM Issue in TiDB with GOMEMLIMIT
  • How To Build a Spring Boot GraalVM Image
  • Master Spring Boot 3 With GraalVM Native Image
  • Best CI/CD Tools for DevOps: A Review of the Top 10

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

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

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: