Sources of Arguments in Parameterized Tests
Learn how to create sources of arguments in JUnit 5.
We'll cover the following
Overview
We’ve discussed built-in sources of parameters for parameterized tests. We can also create new sources of arguments using the @ArgumentsSource
annotation and the org.junit.jupiter.params.provider.ArgumentsProvider
interface. The @ArgumentsSource
annotation specifies the implementation of ArgumentsProvider
to use for providing the arguments. The ArgumentsProvider
only has one method, Stream<? extends Arguments> provideArguments(ExtensionContext context) throws Exception
, to return a stream of Arguments
objects.
The @JsonFileSource
annotation
To demonstrate custom sources of arguments, we’ll create a source of arguments that resolves JSON file arguments. The JSON file should contain an array of objects. Each object in the array is mapped to an Arguments
object. Because the properties in a JSON object aren’t ordered, we need to specify the mapped property name for each parameter.
The @JsonFileSource
annotation uses @ArgumentsSource
to specify the JsonArgumentsProvider
implementation class.
Get hands-on with 1400+ tech skills courses.