javajunit5parametrized-testing

JUnit 5 @ParamterizedTest {arguments} vs {argumentsWithNames} placeholders


I'm using JUnit 5.7.0, IntellijIDEA 2021.1.2 CE, MacOS Catalina 10.15. and don't understand the difference between ParameterizedTest.ARGUMENTS_PLACEHOLDER and ParameterizedTest.ARGUMENTS_WITH_NAMES_PLACEHOLDER.

According to the javadoc ARGUMENTS_WITH_NAMES_PLACEHOLDER deals with named arguments whereas ARGUMENTS_PLACEHOLDER with just arguments. The test result in IDEA for both tests looks the same:

import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;

@ParameterizedTest(name = ARGUMENTS_WITH_NAMES_PLACEHOLDER)
@CsvSource({"apple, 1", "banana, 2", "'lemon, lime', 3" })
void testWithArguments(String fruit, int rank) {
}

@ParameterizedTest(name = ARGUMENTS_PLACEHOLDER)
@CsvSource({"apple, 1", "banana, 2", "'lemon, lime', 3" })
void testWithNamedArguments(String fruit, int rank) {
}

enter image description here

Can anyone provide an example where these two placeholders behave differently?


Solution

  • Solution

    For ARGUMENTS_WITH_NAMES_PLACEHOLDER to work as expected, tests must be compiled with
    -parameters flag. Then, argument names will be 1) stored, and 2) be able to be subsequently retrieved by JUnit via reflection.

    In Practice

    For Java projects built with Gradle, the following would enable this flag specifically for tests:

    tasks.named('compileTestJava') {
        options.compilerArgs += '-parameters'
    }
    

    Reference