JUnit Parameterized Tests allow us to run a test method multiple times with different arguments. JUnit 5 provides a lot of ways to pass parameters to a test method.

JUnit Parameterized Tests

We need following additional dependency to use parameterized tests in our test cases.

We have to use @ParameterizedTest with the test method instead of generic @Test annotation.

We also have to provide a source that will generate the arguments for the method. There are many types of sources we can define and use in our parameterized test methods.

JUnit Parameterized Test with @ValueSource

This is the simplest form of parameterized test, we can use @ValueSource to pass the arguments array. We can pass primitive data types array, string array or class array using ValueSource annotation.

JUnit @ParameterizedTest with @EnumSource

@EnumSource allows us to pass Enums to our test methods.

If we want only specific values from the Enum, we can do that using EnumSource name parameter.

JUnit @ParameterizedTest with @MethodSource

We can use @MethodSource to specify a factory method for test arguments. This method can be present in the same class or any other class too. The factory method should be static and return Strem, Iterator, Iterable or array of elements.

We can also use MethodSource to pass multiple parameters to the test method. In this case, we will have to use Arguments API. Let’s define a separate class with factory method source.

Corresponding JUnit parameterized test method would be defined as:

JUnit MethodSource is very similar to TestNG DataProvider annotation.

JUnit @ParameterizedTest with @CsvSource

We can also pass CSV values to the test method. We can specify the delimiter for multiple arguments in the test method.

JUnit Parameterized Test with CSV File

We can use @CsvFileSource annotation to pass CSV data from a file to the parameterized test method. We can skip the header rows and define our custom delimiter too.

Let’s say we have country_code.csv file defined as:

Here is the test method where CSV file data will be used for arguments mapping.

JUnit Parameterized Tests with Objects

So far we have used primitives and strings in our examples, but in real life, we have to pass objects most of the times. We can use @MethodSource to achieve this functionality.

Let’s say we have a Book class defined as:

Now we can pass Book object to our test methods using below factory method.

Notice that this time I am returning an array of Books, earlier I was returning Stream of elements.

JUnit Parameterized Tests Arguments Verification

If you are running test cases through Eclipse, you can check method arguments to make sure correct values are being passed to the parameterized tests.

junit-parameterized-tests

JUnit Test Methods Argument Conversion

JUnit provides built-in support for many type converters. Some of them are int to long, string to boolean and vice versa, string to enum, date time objects. Below code will also work and JUnit will automatically call our Book class constructor to convert String values to Book object.

However, this could lead to errors when test cases are executed if our Book class constructor changes. It’s better to use MethodSource and provide our own mechanism for object creation.

Summary

JUnit Parameterized Tests were a much-needed feature and it’s good to see so many options to provide arguments to our test methods.

You can check out JUnit example project with complete examples at our GitHub Repository.

By admin

Leave a Reply

%d bloggers like this: