@ArgumentsSource

The last annotation aimed to specify the source of arguments for parameterized tests in JUnit 5 is @ArgumentsSource. With this annotation, we can specify a custom (and reusable in different tests) class, which will contain the parameters for the test. This class must implement the interface org.junit.jupiter.params.provider.ArgumentsProvider.

Let’s see an example. The following class implements a Jupiter parameterized test, in which the arguments source will be defined in the class CustomArgumentsProvider1:

package io.github.bonigarcia;import static org.junit.jupiter.api.Assertions.assertNotNull;import static org.junit.jupiter.api.Assertions.assertTrue;import org.junit.jupiter.params.ParameterizedTest;import org.junit.jupiter.params.provider.ArgumentsSource; ...

Get Mastering Software Testing with JUnit 5 now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.