Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Do not parse {displayName} for @ParameterizedTest using MessageFormat
Prior to this commit, if a @ParameterizedTest used the {displayName} placeholder to generate a display name and the value of the displayName contained an apostrophe (') or something resembling a MessageFormat element (such as {data}), JUnit threw an exception due to failure to parse the display name. This is applicable to method names in Kotlin-based tests or custom display names in general. To fix this bug, instead of replacing the DISPLAY_NAME_PLACEHOLDER before the MessageFormat is evaluated, the DISPLAY_NAME_PLACEHOLDER is now replaced with another temporary placeholder, which is then replaced after the MessageFormat has been evaluated. Closes #3235 Closes #3264
- Loading branch information
1 parent
38c2158
commit 7b2688c
Showing
4 changed files
with
87 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
62 changes: 62 additions & 0 deletions
62
junit-jupiter-params/src/test/kotlin/ParameterizedTestNameFormatterIntegrationTests.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
/* | ||
* Copyright 2015-2023 the original author or authors. | ||
* | ||
* All rights reserved. This program and the accompanying materials are | ||
* made available under the terms of the Eclipse Public License v2.0 which | ||
* accompanies this distribution and is available at | ||
* | ||
* https://www.eclipse.org/legal/epl-v20.html | ||
*/ | ||
import org.junit.jupiter.api.Assertions.assertEquals | ||
import org.junit.jupiter.api.TestInfo | ||
import org.junit.jupiter.params.ParameterizedTest | ||
import org.junit.jupiter.params.provider.ValueSource | ||
|
||
class ParameterizedTestNameFormatterIntegrationTests { | ||
|
||
@ValueSource(strings = ["foo", "bar"]) | ||
@ParameterizedTest | ||
fun `implicit'Name`(param: String, info: TestInfo) { | ||
if (param.equals("foo")) { | ||
assertEquals("[1] foo", info.displayName) | ||
} else { | ||
assertEquals("[2] bar", info.displayName) | ||
} | ||
} | ||
|
||
@ValueSource(strings = ["foo", "bar"]) | ||
@ParameterizedTest(name = "{0}") | ||
fun `zero'Only`(param: String, info: TestInfo) { | ||
if (param.equals("foo")) { | ||
assertEquals("foo", info.displayName) | ||
} else { | ||
assertEquals("bar", info.displayName) | ||
} | ||
} | ||
|
||
@ValueSource(strings = ["foo", "bar"]) | ||
@ParameterizedTest(name = "{displayName}") | ||
fun `displayName'Only`(param: String, info: TestInfo) { | ||
assertEquals("displayName'Only(String, TestInfo)", info.displayName) | ||
} | ||
|
||
@ValueSource(strings = ["foo", "bar"]) | ||
@ParameterizedTest(name = "{displayName} - {0}") | ||
fun `displayName'Zero`(param: String, info: TestInfo) { | ||
if (param.equals("foo")) { | ||
assertEquals("displayName'Zero(String, TestInfo) - foo", info.displayName) | ||
} else { | ||
assertEquals("displayName'Zero(String, TestInfo) - bar", info.displayName) | ||
} | ||
} | ||
|
||
@ValueSource(strings = ["foo", "bar"]) | ||
@ParameterizedTest(name = "{0} - {displayName}") | ||
fun `zero'DisplayName`(param: String, info: TestInfo) { | ||
if (param.equals("foo")) { | ||
assertEquals("foo - zero'DisplayName(String, TestInfo)", info.displayName) | ||
} else { | ||
assertEquals("bar - zero'DisplayName(String, TestInfo)", info.displayName) | ||
} | ||
} | ||
} |