Is it possible to use JUnit5's parameterized new features to run test classes to receive test parameters instead of doing it at method level?
With JUnit 4 a runner such as @RunWith(Parameterized::class)
plus inheritance could be used to pass an array of parameters to subclasses, but I am not sure if it is possible to achieve something equivalent but using the new JUnit 5 api.
Short answer
there's no way to parametrize class creation with JUnit 5 following the style of JUnit 4.
Fortunately, the very intention of separation test logic and test input data (parameters) can be implemented differently.
JUnit 5 has its own approach for making parameterized tests, and, of course, it is different from JUnit 4. The new approach does not allow to use parameterized fixtures at the class level, i.e. through its every test method. So every parameterized test method should be explicitly annotated with a link to parameters.
JUnit 5 provides a plenty of parameter source types, that could be found in documentation and guides
In your case, the simplest way to migrate from
@Parameters
of Junit 4 is using@MethodSource
or@ArgumentsSource
oforg.junit.jupiter.params.provider.*
.JUnit 4:
JUnit 5 (with
@MethodSource
):JUnit 5 (with
@ArgumentsSource
):Consider that a method in
@MethodSource
and a class in@ArgumentsSource
could be described anywhere, not only inside the same class where the test methods are located. Also@MethodSource
allows to provide multiple source methods, since itsvalue
is aString[]
.Some remarks and comparison
In JUnit 4 we could only have a single factory method providing parameters, and the tests were supposed to be built around those parameters. On the contrary, JUnit 5 gives more abstraction and flexibility in binding parameters and decouples test logic from its parameters, which are secondary. That allows building tests independently from parameter sources, and easily change them when needed.
Dependency requirement
Parameterized tests feature is not included in the core
junit-jupiter-engine
, but is located in a separate dependencyjunit-jupiter-params
.