@MethodSource and @CsvSource

Learn how to use the @MethodSource and @CsvSource arguments in JUnit 5.

@MethodSource

The org.junit.jupiter.params.provider.MethodSource annotation uses one or more methods in the test class to provide the arguments. The methods must have no arguments and return a Stream, Iterable, Iterator, or array of arguments. The methods must be static unless they use an instance life cycle.

In the code below, we use simpleMethod() to test assertNotNull() by passing the string we used in the names() method. It passes because the str isn’t null.

Get hands-on with 1400+ tech skills courses.