Generating display names for @ParameterizedTest in JUnit 5

As of JUnit 5.8.0, there is a Named<T> interface as part of the JUnit Jupiter API with “automatic support for injecting the contained payload [the arguments] into parameterized methods directly” (see issue #2301). Example:

@DisplayName("A parameterized test with named arguments")
@ParameterizedTest
@MethodSource("namedArguments")
void testWithNamedArguments(File file) {}

static Stream<Arguments> namedArguments() {
    return Stream.of(
        Arguments.of(Named.of("An important file", new File("path1"))),
        Arguments.of(Named.of("Another file", new File("path2")))
    );
}

If you prefer static imports, you can also go for the corresponding aliases from Arguments and Named:

arguments(named("An important file", new File("path1")))

For more information, please refer to the corresponding docs.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)