Skip to content

Commit 8770829

Browse files
committed
Consistently use "name = value" arguments format in parameterized tests
Prior to this commit, name-value pairs for arguments in display names for parameterized classes and tests had different formatting/spacing depending on the use case. When using the `useHeadersInDisplayName` feature of @⁠CsvSource or @⁠CsvFileSource, they took the form "name = value"; whereas, when using the {argumentsWithNames} display name pattern, they took the form "name=value" (without spaces). With this commit, we now consistently use the "name = value" formatting (with spaces). Closes #4786
1 parent 9a036ce commit 8770829

File tree

8 files changed

+183
-168
lines changed

8 files changed

+183
-168
lines changed

documentation/src/docs/asciidoc/release-notes/release-notes-6.0.0-M2.adoc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,9 @@ repository on GitHub.
9595
[[release-notes-6.0.0-M2-junit-jupiter-new-features-and-improvements]]
9696
==== New Features and Improvements
9797

98+
* Display names for `@ParameterizedClass` and `@ParameterizedTest` now consistently style
99+
name-value pairs for arguments using `name = value` formatting – for example,
100+
`fruit = apple` instead of `fruit=apple`.
98101
* Reason strings supplied to `ConditionEvaluationResult` APIs are now officially declared
99102
as `@Nullable`.
100103
* Introduce `computeIfAbsent(...)` methods in `ExtensionContext.Store` to ease simplify

documentation/src/docs/asciidoc/user-guide/writing-tests.adoc

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1242,26 +1242,26 @@ Executing the above test class yields the following output:
12421242

12431243
....
12441244
FruitTests ✔
1245-
├─ [1] fruit=apple ✔
1245+
├─ [1] fruit = apple ✔
12461246
│ └─ QuantityTests ✔
1247-
│ ├─ [1] quantity=23 ✔
1247+
│ ├─ [1] quantity = 23 ✔
12481248
│ │ └─ test(Duration) ✔
1249-
│ │ ├─ [1] duration=PT1H ✔
1250-
│ │ └─ [2] duration=PT2H ✔
1251-
│ └─ [2] quantity=42 ✔
1249+
│ │ ├─ [1] duration = PT1H ✔
1250+
│ │ └─ [2] duration = PT2H ✔
1251+
│ └─ [2] quantity = 42 ✔
12521252
│ └─ test(Duration) ✔
1253-
│ ├─ [1] duration=PT1H ✔
1254-
│ └─ [2] duration=PT2H ✔
1255-
└─ [2] fruit=banana ✔
1253+
│ ├─ [1] duration = PT1H ✔
1254+
│ └─ [2] duration = PT2H ✔
1255+
└─ [2] fruit = banana ✔
12561256
└─ QuantityTests ✔
1257-
├─ [1] quantity=23 ✔
1257+
├─ [1] quantity = 23 ✔
12581258
│ └─ test(Duration) ✔
1259-
│ ├─ [1] duration=PT1H ✔
1260-
│ └─ [2] duration=PT2H ✔
1261-
└─ [2] quantity=42 ✔
1259+
│ ├─ [1] duration = PT1H ✔
1260+
│ └─ [2] duration = PT2H ✔
1261+
└─ [2] quantity = 42 ✔
12621262
└─ test(Duration) ✔
1263-
├─ [1] duration=PT1H ✔
1264-
└─ [2] duration=PT2H ✔
1263+
├─ [1] duration = PT1H ✔
1264+
└─ [2] duration = PT2H ✔
12651265
....
12661266

12671267
[[writing-tests-dependency-injection]]
@@ -1649,9 +1649,9 @@ following.
16491649

16501650
....
16511651
palindromes(String) ✔
1652-
├─ [1] candidate=racecar ✔
1653-
├─ [2] candidate=radar ✔
1654-
└─ [3] candidate=able was I ere I saw elba ✔
1652+
├─ [1] candidate = racecar ✔
1653+
├─ [2] candidate = radar ✔
1654+
└─ [3] candidate = able was I ere I saw elba ✔
16551655
....
16561656

16571657
The same `@ValueSource` annotation can be used to specify the source of arguments for a
@@ -1668,13 +1668,13 @@ following.
16681668

16691669
....
16701670
PalindromeTests ✔
1671-
├─ [1] candidate=racecar ✔
1671+
├─ [1] candidate = racecar ✔
16721672
│ ├─ palindrome() ✔
16731673
│ └─ reversePalindrome() ✔
1674-
├─ [2] candidate=radar ✔
1674+
├─ [2] candidate = radar ✔
16751675
│ ├─ palindrome() ✔
16761676
│ └─ reversePalindrome() ✔
1677-
└─ [3] candidate=able was I ere I saw elba ✔
1677+
└─ [3] candidate = able was I ere I saw elba ✔
16781678
├─ palindrome() ✔
16791679
└─ reversePalindrome() ✔
16801680
....
@@ -2332,10 +2332,10 @@ The following listing shows the generated display names for the first two parame
23322332
test methods above.
23332333

23342334
----
2335-
[1] country=Sweden, reference=1
2336-
[2] country=Poland, reference=2
2337-
[3] country=United States of America, reference=3
2338-
[4] country=France, reference=700_000
2335+
[1] country = Sweden, reference = 1
2336+
[2] country = Poland, reference = 2
2337+
[3] country = United States of America, reference = 3
2338+
[4] country = France, reference = 700_000
23392339
----
23402340

23412341
The following listing shows the generated display names for the last parameterized test

junit-jupiter-params/src/main/java/org/junit/jupiter/params/ParameterizedInvocationNameFormatter.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -187,8 +187,9 @@ private PartialFormatters createPartialFormatters(String displayName,
187187
private static String argumentsWithNamesPattern(int length, ParameterizedDeclarationContext<?> declarationContext) {
188188
ResolverFacade resolverFacade = declarationContext.getResolverFacade();
189189
return IntStream.range(0, length) //
190-
.mapToObj(index -> resolverFacade.getParameterName(index).map(name -> name + "=").orElse("") + "{"
191-
+ index + "}") //
190+
.mapToObj(index -> resolverFacade.getParameterName(index)//
191+
.map(name -> name + " = ").orElse("") //
192+
+ "{" + index + "}") //
192193
.collect(joining(", "));
193194
}
194195

jupiter-tests/src/test/java/org/junit/jupiter/params/ParameterizedClassIntegrationTests.java

Lines changed: 48 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ void injectsParametersIntoClass(Class<?> classTemplateClass) {
124124

125125
var results = executeTestsForClass(classTemplateClass);
126126

127-
String parameterNamePrefix = classTemplateClass.getSimpleName().contains("Aggregator") ? "" : "value=";
127+
String parameterNamePrefix = classTemplateClass.getSimpleName().contains("Aggregator") ? "" : "value = ";
128128

129129
results.allEvents().assertEventsMatchExactly( //
130130
event(engine(), started()), //
@@ -176,7 +176,7 @@ void supportsNullAndEmptySource(Class<?> classTemplateClass) {
176176

177177
results.allEvents().assertStatistics(stats -> stats.started(6).succeeded(6));
178178
assertThat(invocationDisplayNames(results)) //
179-
.containsExactly("[1] value=null", "[2] value=");
179+
.containsExactly("[1] value = null", "[2] value = ");
180180
}
181181

182182
@ParameterizedTest
@@ -188,8 +188,8 @@ void supportsCsvFileSource(Class<?> classTemplateClass) {
188188

189189
results.allEvents().assertStatistics(stats -> stats.started(10).succeeded(10));
190190
assertThat(invocationDisplayNames(results)) //
191-
.containsExactly("[1] name=foo, value=1", "[2] name=bar, value=2", "[3] name=baz, value=3",
192-
"[4] name=qux, value=4");
191+
.containsExactly("[1] name = foo, value = 1", "[2] name = bar, value = 2",
192+
"[3] name = baz, value = 3", "[4] name = qux, value = 4");
193193
}
194194

195195
@ParameterizedTest
@@ -201,7 +201,7 @@ void supportsSingleEnumSource(Class<?> classTemplateClass) {
201201

202202
results.allEvents().assertStatistics(stats -> stats.started(4).succeeded(4));
203203
assertThat(invocationDisplayNames(results)) //
204-
.containsExactly("[1] value=FOO");
204+
.containsExactly("[1] value = FOO");
205205
}
206206

207207
@ParameterizedTest
@@ -213,7 +213,7 @@ void supportsRepeatedEnumSource(Class<?> classTemplateClass) {
213213

214214
results.allEvents().assertStatistics(stats -> stats.started(6).succeeded(6));
215215
assertThat(invocationDisplayNames(results)) //
216-
.containsExactly("[1] value=FOO", "[2] value=BAR");
216+
.containsExactly("[1] value = FOO", "[2] value = BAR");
217217
}
218218

219219
@ParameterizedTest
@@ -225,7 +225,7 @@ void supportsMethodSource(Class<?> classTemplateClass) {
225225

226226
results.allEvents().assertStatistics(stats -> stats.started(6).succeeded(6));
227227
assertThat(invocationDisplayNames(results)) //
228-
.containsExactly("[1] value=foo", "[2] value=bar");
228+
.containsExactly("[1] value = foo", "[2] value = bar");
229229
}
230230

231231
@Test
@@ -247,7 +247,7 @@ void supportsFieldSource(Class<?> classTemplateClass) {
247247

248248
results.allEvents().assertStatistics(stats -> stats.started(6).succeeded(6));
249249
assertThat(invocationDisplayNames(results)) //
250-
.containsExactly("[1] value=foo", "[2] value=bar");
250+
.containsExactly("[1] value = foo", "[2] value = bar");
251251
}
252252

253253
@Test
@@ -269,7 +269,7 @@ void supportsArgumentsSource(Class<?> classTemplateClass) {
269269

270270
results.allEvents().assertStatistics(stats -> stats.started(6).succeeded(6));
271271
assertThat(invocationDisplayNames(results)) //
272-
.containsExactly("[1] value=foo", "[2] value=bar");
272+
.containsExactly("[1] value = foo", "[2] value = bar");
273273
}
274274

275275
@Test
@@ -305,7 +305,7 @@ void supportsCustomNamePatterns() {
305305

306306
results.allEvents().assertStatistics(stats -> stats.started(6).succeeded(6));
307307
assertThat(invocationDisplayNames(results)) //
308-
.containsExactly("1 | TesT | 1, foo | set", "2 | TesT | 2, bar | number=2, name=bar");
308+
.containsExactly("1 | TesT | 1, foo | set", "2 | TesT | 2, bar | number = 2, name = bar");
309309
}
310310

311311
@Test
@@ -398,65 +398,65 @@ void supportsNestedParameterizedClass(Class<?> classTemplateClass) {
398398
results.testEvents().assertStatistics(stats -> stats.started(8).succeeded(8));
399399
assertThat(invocationDisplayNames(results)) //
400400
.containsExactly( //
401-
"[1] number=1", "[1] text=foo", "[2] text=bar", //
402-
"[2] number=2", "[1] text=foo", "[2] text=bar" //
401+
"[1] number = 1", "[1] text = foo", "[2] text = bar", //
402+
"[2] number = 2", "[1] text = foo", "[2] text = bar" //
403403
);
404404
assertThat(allReportEntries(results)).map(it -> it.get("value")).containsExactly(
405405
// @formatter:off
406406
"beforeAll: %s".formatted(classTemplateClass.getSimpleName()),
407407
"beforeParameterizedClassInvocation: %s".formatted(classTemplateClass.getSimpleName()),
408408
"beforeAll: InnerTestCase",
409409
"beforeParameterizedClassInvocation: InnerTestCase",
410-
"beforeEach: [1] flag=true [%s]".formatted(classTemplateClass.getSimpleName()),
411-
"beforeEach: [1] flag=true [InnerTestCase]",
410+
"beforeEach: [1] flag = true [%s]".formatted(classTemplateClass.getSimpleName()),
411+
"beforeEach: [1] flag = true [InnerTestCase]",
412412
"test(1, foo, true)",
413-
"afterEach: [1] flag=true [InnerTestCase]",
414-
"afterEach: [1] flag=true [%s]".formatted(classTemplateClass.getSimpleName()),
415-
"beforeEach: [2] flag=false [%s]".formatted(classTemplateClass.getSimpleName()),
416-
"beforeEach: [2] flag=false [InnerTestCase]",
413+
"afterEach: [1] flag = true [InnerTestCase]",
414+
"afterEach: [1] flag = true [%s]".formatted(classTemplateClass.getSimpleName()),
415+
"beforeEach: [2] flag = false [%s]".formatted(classTemplateClass.getSimpleName()),
416+
"beforeEach: [2] flag = false [InnerTestCase]",
417417
"test(1, foo, false)",
418-
"afterEach: [2] flag=false [InnerTestCase]",
419-
"afterEach: [2] flag=false [%s]".formatted(classTemplateClass.getSimpleName()),
418+
"afterEach: [2] flag = false [InnerTestCase]",
419+
"afterEach: [2] flag = false [%s]".formatted(classTemplateClass.getSimpleName()),
420420
"afterParameterizedClassInvocation: InnerTestCase",
421421
"beforeParameterizedClassInvocation: InnerTestCase",
422-
"beforeEach: [1] flag=true [%s]".formatted(classTemplateClass.getSimpleName()),
423-
"beforeEach: [1] flag=true [InnerTestCase]",
422+
"beforeEach: [1] flag = true [%s]".formatted(classTemplateClass.getSimpleName()),
423+
"beforeEach: [1] flag = true [InnerTestCase]",
424424
"test(1, bar, true)",
425-
"afterEach: [1] flag=true [InnerTestCase]",
426-
"afterEach: [1] flag=true [%s]".formatted(classTemplateClass.getSimpleName()),
427-
"beforeEach: [2] flag=false [%s]".formatted(classTemplateClass.getSimpleName()),
428-
"beforeEach: [2] flag=false [InnerTestCase]",
425+
"afterEach: [1] flag = true [InnerTestCase]",
426+
"afterEach: [1] flag = true [%s]".formatted(classTemplateClass.getSimpleName()),
427+
"beforeEach: [2] flag = false [%s]".formatted(classTemplateClass.getSimpleName()),
428+
"beforeEach: [2] flag = false [InnerTestCase]",
429429
"test(1, bar, false)",
430-
"afterEach: [2] flag=false [InnerTestCase]",
431-
"afterEach: [2] flag=false [%s]".formatted(classTemplateClass.getSimpleName()),
430+
"afterEach: [2] flag = false [InnerTestCase]",
431+
"afterEach: [2] flag = false [%s]".formatted(classTemplateClass.getSimpleName()),
432432
"afterParameterizedClassInvocation: InnerTestCase",
433433
"afterAll: InnerTestCase",
434434
"afterParameterizedClassInvocation: %s".formatted(classTemplateClass.getSimpleName()),
435435
"beforeParameterizedClassInvocation: %s".formatted(classTemplateClass.getSimpleName()),
436436
"beforeAll: InnerTestCase",
437437
"beforeParameterizedClassInvocation: InnerTestCase",
438-
"beforeEach: [1] flag=true [%s]".formatted(classTemplateClass.getSimpleName()),
439-
"beforeEach: [1] flag=true [InnerTestCase]",
438+
"beforeEach: [1] flag = true [%s]".formatted(classTemplateClass.getSimpleName()),
439+
"beforeEach: [1] flag = true [InnerTestCase]",
440440
"test(2, foo, true)",
441-
"afterEach: [1] flag=true [InnerTestCase]",
442-
"afterEach: [1] flag=true [%s]".formatted(classTemplateClass.getSimpleName()),
443-
"beforeEach: [2] flag=false [%s]".formatted(classTemplateClass.getSimpleName()),
444-
"beforeEach: [2] flag=false [InnerTestCase]",
441+
"afterEach: [1] flag = true [InnerTestCase]",
442+
"afterEach: [1] flag = true [%s]".formatted(classTemplateClass.getSimpleName()),
443+
"beforeEach: [2] flag = false [%s]".formatted(classTemplateClass.getSimpleName()),
444+
"beforeEach: [2] flag = false [InnerTestCase]",
445445
"test(2, foo, false)",
446-
"afterEach: [2] flag=false [InnerTestCase]",
447-
"afterEach: [2] flag=false [%s]".formatted(classTemplateClass.getSimpleName()),
446+
"afterEach: [2] flag = false [InnerTestCase]",
447+
"afterEach: [2] flag = false [%s]".formatted(classTemplateClass.getSimpleName()),
448448
"afterParameterizedClassInvocation: InnerTestCase",
449449
"beforeParameterizedClassInvocation: InnerTestCase",
450-
"beforeEach: [1] flag=true [%s]".formatted(classTemplateClass.getSimpleName()),
451-
"beforeEach: [1] flag=true [InnerTestCase]",
450+
"beforeEach: [1] flag = true [%s]".formatted(classTemplateClass.getSimpleName()),
451+
"beforeEach: [1] flag = true [InnerTestCase]",
452452
"test(2, bar, true)",
453-
"afterEach: [1] flag=true [InnerTestCase]",
454-
"afterEach: [1] flag=true [%s]".formatted(classTemplateClass.getSimpleName()),
455-
"beforeEach: [2] flag=false [%s]".formatted(classTemplateClass.getSimpleName()),
456-
"beforeEach: [2] flag=false [InnerTestCase]",
453+
"afterEach: [1] flag = true [InnerTestCase]",
454+
"afterEach: [1] flag = true [%s]".formatted(classTemplateClass.getSimpleName()),
455+
"beforeEach: [2] flag = false [%s]".formatted(classTemplateClass.getSimpleName()),
456+
"beforeEach: [2] flag = false [InnerTestCase]",
457457
"test(2, bar, false)",
458-
"afterEach: [2] flag=false [InnerTestCase]",
459-
"afterEach: [2] flag=false [%s]".formatted(classTemplateClass.getSimpleName()),
458+
"afterEach: [2] flag = false [InnerTestCase]",
459+
"afterEach: [2] flag = false [%s]".formatted(classTemplateClass.getSimpleName()),
460460
"afterParameterizedClassInvocation: InnerTestCase",
461461
"afterAll: InnerTestCase",
462462
"afterParameterizedClassInvocation: %s".formatted(classTemplateClass.getSimpleName()),
@@ -2090,14 +2090,14 @@ static void before2(ArgumentsAccessor accessor) {
20902090

20912091
@BeforeParameterizedClassInvocation
20922092
static void before3(AtomicInteger value, TestInfo testInfo) {
2093-
assertEquals("[1] value=1", testInfo.getDisplayName());
2093+
assertEquals("[1] value = 1", testInfo.getDisplayName());
20942094
value.incrementAndGet();
20952095
}
20962096

20972097
@BeforeParameterizedClassInvocation
20982098
static void before4(ArgumentsAccessor accessor, TestInfo testInfo) {
20992099
assertEquals(1, accessor.getInteger(0));
2100-
assertEquals("[1] value=1", testInfo.getDisplayName());
2100+
assertEquals("[1] value = 1", testInfo.getDisplayName());
21012101
}
21022102

21032103
@BeforeParameterizedClassInvocation
@@ -2109,7 +2109,7 @@ static void before4(AtomicInteger value, ArgumentsAccessor accessor) {
21092109
@BeforeParameterizedClassInvocation
21102110
static void before5(AtomicInteger value, ArgumentsAccessor accessor, TestInfo testInfo) {
21112111
assertEquals(1, accessor.getInteger(0));
2112-
assertEquals("[1] value=1", testInfo.getDisplayName());
2112+
assertEquals("[1] value = 1", testInfo.getDisplayName());
21132113
value.incrementAndGet();
21142114
}
21152115

@@ -2122,7 +2122,7 @@ static void before6(@TimesTwo int valueTimesTwo) {
21222122
static void after(AtomicInteger value, ArgumentsAccessor accessor, TestInfo testInfo) {
21232123
assertEquals(6, value.get());
21242124
assertEquals(1, accessor.getInteger(0));
2125-
assertEquals("[1] value=1", testInfo.getDisplayName());
2125+
assertEquals("[1] value = 1", testInfo.getDisplayName());
21262126
}
21272127
}
21282128

jupiter-tests/src/test/java/org/junit/jupiter/params/ParameterizedInvocationNameFormatterTests.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ void formatsCompleteArgumentsListWithNames() {
140140
var formatter = formatter(ARGUMENTS_WITH_NAMES_PLACEHOLDER, "enigma", testMethod);
141141

142142
var formattedName = format(formatter, 1, arguments(42, "enigma", new Object[] { "foo", 1 }));
143-
assertEquals("someNumber=42, someString=enigma, someArray=[foo, 1]", formattedName);
143+
assertEquals("someNumber = 42, someString = enigma, someArray = [foo, 1]", formattedName);
144144
}
145145

146146
@Test
@@ -149,7 +149,7 @@ void formatsCompleteArgumentsListWithoutNamesForAggregators() {
149149
var formatter = formatter(ARGUMENTS_WITH_NAMES_PLACEHOLDER, "enigma", testMethod);
150150

151151
var formattedName = format(formatter, 1, arguments(42, "foo", "bar"));
152-
assertEquals("someNumber=42, foo, bar", formattedName);
152+
assertEquals("someNumber = 42, foo, bar", formattedName);
153153
}
154154

155155
@Test
@@ -318,7 +318,7 @@ void mixedTypesOfArgumentsImplementationsAndCustomDisplayNamePattern() {
318318
var name2 = format(formatter, 2, arguments("apple", "banana"));
319319

320320
assertThat(name1).isEqualTo("[1] Mixed Arguments Types :: Fruits");
321-
assertThat(name2).isEqualTo("[2] Mixed Arguments Types :: fruit1=apple, fruit2=banana");
321+
assertThat(name2).isEqualTo("[2] Mixed Arguments Types :: fruit1 = apple, fruit2 = banana");
322322
}
323323

324324
}

0 commit comments

Comments
 (0)