When to use parameterized tests
Parameterized tests help you to reduce the amount of testing code you need for test cases that only need different input and or output. Instead of copying a test method multiple times, you only have to write two methods. One to test the business logic and one to supply the test with parameters.
Dependencies
You need two dependencies in your POM for parameterized tests to work. You need a recent version (5.7 or higher) of the junit engine and one for the parameterized tests self.
|
|
If you are already have a recent version of junit you only have to add this dependency.
|
|
Example
This is the method we are going to test. It performs addition on two numbers and returns the result.
|
|
Below is the method that we are going to use to supply our test method with input values. The numbers in the Argument.of(...)
correspond with the parameters of the test method. When we look at Arguments.of(2, 1, 3)
the 2
is firstInteger
, 1
is secondInteger
and 3
is expectedResult
.
|
|
To make a testing method a parameterized test we need to add two annotations @ParameterizedTest
and @MethodSource
.
@ParameterizedTest
tells junit the annotated method is a parameterized test method.@MethodSource
tells junit where to get the parameters from.
|
|
Changing the names of the tests
When we run the test, we see a result similar to the one below. At first glance, it is not clear what the purpose of the test was.
|
|
We can make the test output a lot clearer by providing @ParameterizedTest
with a name. We can set it with a String
that uses
{index number}
to access the used parameters.
|
|
This gives us the following output:
|
|
Now when a test fails, you see immediately what the input is and what the expected result is.
Conclusion
Parameterized tests help you to write more maintainable code for test cases that only differ in input or output.
Further reading
More about testing in Java: