Introduction
In this blog we will learn how to use Junit 5 Parameterized tests.
Junit5 parameterized tests are used to run same test case for multiple inputs for verifying the robustness of the logic.
Let us say you want to check if the algorithm for prime number is working as expected for given list of input numbers.
We must have source to provide the data for testing the method.
There are multiple sources from which we can provide input data.
- ValueSource
- EnumSource
- CsvSource
- CsvFileSource
- MethodSource
To get started with parameterised test, we must provide maven dependency for the same.
I am using java 17 and below are the maven dependencies required.
|
|
All the test cases should be annotated with @ParameterizedTest
and not @Test
ValueSource
Using @ValueSource we can pass array of values of different data types for testing.
|
|
Above test cases runs 4 times, each time value from the array is mapped to method parameter which is int number
.
@ValueSource can have other data types like int,short,byte, double,float,char,String and Class.
Below is one more example where we are using strings as value source.
|
|
EnumSource
If you want to test your logic based on the values in enum you can use @EnumSource.
To explain this I have taken example of DayOfWeek
inbuilt enum which has values of each day in a week.
|
|
In the above example each day has value from 1-Monday to 7-Sunday.
Above test case is going to run 7 times with each value of week and check if it is between 1 and 7(both inclusive).
CsvSource
If we want to pass multiple arguments to the method for testing which includes input value and expected result we can
use @CsvSource
for the same
@CsvSource
takes array of values separated by comma just like a line in a CSV file.
Each entry is separated by comma and each value is mapped to method arguments.
|
|
In the above example we check if input value is converted to uppercase(expected).
Here first value harish
is input value and HARISH
is the expected value.
In the above example, comma is the default separator. You can use you own delimiter as shown below.
|
|
CsvFileSource
We can pass CSV values required for testing from the file itself using @CsvFileSource
.
This can come handy when input values are huge in numbers to be tested.
Ensure your csv file which contains testing data is present in classpath.
|
|
In @CsvFileSource
we can mention file name in resources attribute.
names.csv contains following content.
|
|
Method Source
Instead of providing input value as part of annotations we can use method to pass input value.
If the input values are in large number, specifying them in the annotations is not the good idea.
Using @MethodSource
we just need to pass method name as part of arguments.
In the below example again we are testing whether string values are in uppercase as in @CsvSource
.
But here we are using @MethodSource
to take input value from the method itself.
|
|
Method provideStrings
returns stream of arguments.
First argument matched with the input value and second value of Arguments matches with expected value.
Conclusion
Thus we can have seen how to use parameterized test cases using multiple sources.
Hope this was helpful!