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) {
}
Can anyone provide an example where these two placeholders behave differently?
Solution 1:
If you use @ParameterizedTest(name = ARGUMENTS_WITH_NAMES_PLACEHOLDER)
you'll get output result with parameterName
+ "=" + paraveterValue
.
But if you use @ParameterizedTest(name = ARGUMENTS_PLACEHOLDER)
you'll get only paraveterValue
Also, you can see that in javadoc:
Placeholder for the complete, comma-separated named arguments list of the current invocation of a @ParameterizedTest method: {argumentsWithNames}
@API(status = EXPERIMENTAL, since = "5.6")
String ARGUMENTS_WITH_NAMES_PLACEHOLDER = "{argumentsWithNames}";
Default display name pattern for the current invocation of a @ParameterizedTest method: "[{index}] {argumentsWithNames}"
Note that the default pattern does not include the display name of the @ParameterizedTest method.
@API(status = EXPERIMENTAL, since = "5.3")
String DEFAULT_DISPLAY_NAME = "[" + INDEX_PLACEHOLDER + "] " + ARGUMENTS_WITH_NAMES_PLACEHOLDER;
I tried to run your code like that:
@ParameterizedTest(name = ARGUMENTS_WITH_NAMES_PLACEHOLDER)
@CsvSource({"apple, 1", "banana, 2", "'lemon, lime', 3" })
void testWithNamedArguments(String fruit, int rank) {
}
and I got the correct result:
I use jupiter 5.6.3, the feature ARGUMENTS_WITH_NAMES_PLACEHOLDER appeared since 5.6 version.