I read in maven-surefire-plugin and default locale that Maven runs tests forked and thus might lose any locale you might have set.
Is there a way to run tests in Maven in forked mode and still retain locale?
-- EDIT --
So, to clarify a bit: It is fully possible to set language and region in System Properties using:
<systemPropertyVariables>
<user.language>en</user.language>
<user.region>GB</user.region>
</systemPropertyVariables>
And they are actually passed to the running process. This does not however set locale accordingly; locale remains as System Default.
The Surefire Plugin is used during the test phase of the build lifecycle to execute the unit tests of an application. It generates reports in two different file formats: Plain text files (*. txt)
systemPropertyVariables. Maven Surefire plugin provides the configuration parameter systemPropertyVariables to set system properties. The properties defined here will be available in the unit tests.
Try this:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<argLine>-Duser.language=en -Duser.region=GB</argLine>
</configuration>
</plugin>
I don't have a way to test this, but give it a try:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.9</version>
<configuration>
<project>
<properties>
<user.language>en</user.language>
<user.region>GB</user.region>
</properties>
</project>
<includes>
<include>**/*Test.java</include>
</includes>
<forkMode>pertest</forkMode>
</configuration>
</plugin>
EDIT: OK try this:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.9</version>
<configuration>
<systemPropertyVariables>
<user.language>en</user.language>
<user.region>GB</user.region>
</systemPropertyVariables>
<includes>
<include>**/*Test.java</include>
</includes>
<forkMode>pertest</forkMode>
</configuration>
</plugin>
The default locale of your application is determined in three ways. First, unless you have explicitly changed the default, the getDefault() method returns the locale that was initially determined by the Java Virtual Machine (JVM) when it first loaded. That is, the JVM determines the default locale from the host environment. The host environment's locale is determined by the host operating system and the user preferences established on that system.
Second, on some Java runtime implementations, the application user can override the host's default locale by providing this information on the command line by setting the user.language, user.country, and user.variant system properties. [Source]
I think you are a victim of the first part, so second never gets a chance.
Instead, what you can do is in your unit test (or maybe a base class thereof) set the default locale programatically as stated later in the same text:
Third, your application can call the
setDefault(Locale aLocale)
method. ThesetDefault(Locale aLocale)
method lets your application set a systemwide resource. After you set the default locale with this method, subsequent calls toLocale.getDefault()
will return the newly set locale.
static{
Locale.setDefault(Locale.UK);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With