me1111
me1111

Reputation: 1157

Change locale for JUnit tests via Spring context

All tests are launched by gradle. I'd like to define the locale for all JUnit tests on a project. At first I thought about the following way:

public class TestCases {
    static Locale defaultLocale = Locale.getDefault();

    @BeforeClass
    public static void setDefaultLocale() {
        Locale.setDefault(Locale.UK);
    }

    // here goes bunch of tests
    // ...

    @AfterClass
    public static void restoreLocale() {
        Locale.setDefault(defaultLocale);
    }
}

But it is too cumbersome, as I have hundreds of files to be changed. I found also that running single test with -Duser.language=en parameter (I use Intellij) will do the job. But I am not able to change gradle scripts in order to provide this solution. Is there any way to define Locale for JUnit tests via Spring context? Or maybe there is an other better way? Thanks.

Upvotes: 4

Views: 6522

Answers (1)

Benjamin Muschko
Benjamin Muschko

Reputation: 33456

One way to achieve this would be to add a test lifecycle hook that is fired before your suite is executed.

test.beforeSuite { TestDescriptor suite ->
   System.setProperty('user.language', 'en')
}

Upvotes: 4

Related Questions