Simple Parameterized Test
Understand how to use parameterized tests with a simple and custom display name.
We'll cover the following
Simple display names
In JUnit 5, we can use @RepeatedTest
to make a test run multiple times. However, all these repetitions are identical. The @RepeatedTest
simply runs the same test over and over. To run a test multiple times differently, we can use the parameterized tests.
Just as with @RepeatedTest
, the org.junit.jupiter.params.ParameterizedTest
annotation can be used to replace @Test
. The test method should have at least one parameter. We also need to provide at least one source to provide the actual arguments for each invocation. In the code below, the test method simpleValue
has an int parameter, num
, and we assert that num
should be greater than zero. The org.junit.jupiter.params.provider.ValueSource
annotation provides static values for arguments. We provide three integers here.
In the code below, in the simpleValue()
function, assertTrue()
checks the condition. If it’s num%2 == 0
, the condition passes. Otherwise, it fails.
Get hands-on with 1400+ tech skills courses.