Skip to content

ParameterizedTest formatting is Locale-sensitive #2417

@johnsoneal

Description

@johnsoneal

ParameterizedTest with name pattern specified relies on Locale.getDefault(). This results in test names that possibly changing:

public class SimplifiedExample
{
    @BeforeAll
    static void before_all() { Locale.setDefault(Locale.US); }

    @BeforeEach
    void before_each() { Locale.setDefault(Locale.UK); }

    @ParameterizedTest(name = "{0,number,currency}")
    @ValueSource(ints = { 1_000, 2_000 })
    void test1(int a)
    {
        // Test names with locale change:
        // $1,000.00
        // £2,000.00
    }
}

It seems the first MessageFormat is created after the call to BeforeAll and before the call to BeforeEach. As the Locale cannot be set for a ParameterizedTest your stuck with whatever the environments current default is.

It seems that the limited formatting is already down the "rabbit hole" @marcphilipp #1154

Add to this that the arguments formatted are those supplied by the provider before implicit or explicit conversion and not those supplied to the method then formatting is not very useful and certainly breaks the principle of least surprise.

As such I propose that formatting should support only place holders '{0} {1} etc...' until such time that @sbrannen #1154 support for formatting API is added.

Activity

added this to the 5.8 M1 milestone on Sep 22, 2020
sbrannen

sbrannen commented on Sep 22, 2020

@sbrannen
Member

Tentatively slated for 5.8 M1 for team discussion

changed the title [-]ParameterizedTest formatting Locale sensative durring test[/-] [+]ParameterizedTest formatting is Locale-sensitive[/+] on Sep 22, 2020
marcphilipp

marcphilipp commented on Oct 30, 2020

@marcphilipp
Member

Team decision: As documented, we pass the format expression to MessageFormat which uses the default locale. Thus, this works as designed and documented. If you run your tests on machines with different locales and want consistent test names, please set the JDK's locale via the standard system property. If you need to test with different locales, you could use Pioneer's @DefaultLocale.

removed this from the 5.8 M1 milestone on Oct 30, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @sbrannen@marcphilipp@johnsoneal

        Issue actions

          ParameterizedTest formatting is Locale-sensitive · Issue #2417 · junit-team/junit-framework