Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set surefire plugin Locale in Maven pom file

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.

like image 841
JHollanti Avatar asked Jan 17 '12 21:01

JHollanti


People also ask

What depends on the surefire plugin of Maven?

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)

What is systemPropertyVariables in POM XML?

systemPropertyVariables. Maven Surefire plugin provides the configuration parameter systemPropertyVariables to set system properties. The properties defined here will be available in the unit tests.


3 Answers

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>
like image 126
MIURA Toru Avatar answered Oct 12 '22 11:10

MIURA Toru


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>
like image 25
Jonathan S. Fisher Avatar answered Oct 12 '22 10:10

Jonathan S. Fisher


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. The setDefault(Locale aLocale) method lets your application set a systemwide resource. After you set the default locale with this method, subsequent calls to Locale.getDefault() will return the newly set locale.

static{
        Locale.setDefault(Locale.UK);
}
like image 24
Sean Patrick Floyd Avatar answered Oct 12 '22 11:10

Sean Patrick Floyd