JHollanti
JHollanti

Reputation: 2413

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.

Upvotes: 20

Views: 13545

Answers (4)

malloc4k
malloc4k

Reputation: 1937

I had the same issue, but I had to solve it without ingerence to the pom.xml file. This is possible through Maven's global configuration file (usually located at ~/.m2/settings.xml). And to do it you add a profile like below, that will be activated by default:

<?xml version="1.0" encoding="UTF-8"?>
<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0
        http://maven.apache.org/xsd/settings-1.0.0.xsd">

    ...

    <profiles>
        <profile>    
            <id>my-prof</id>
            <properties>
                <argLine>-Duser.language=en -Duser.region=GB</argLine>
            </properties>            
        </profile>
    </profiles>

    <activeProfiles>
        <activeProfile>my-prof</activeProfile>
    </activeProfiles>

    ...

</settings>

Upvotes: 2

MIURA Toru
MIURA Toru

Reputation: 251

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>

Upvotes: 25

Sean Patrick Floyd
Sean Patrick Floyd

Reputation: 299058

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);
}

Upvotes: 2

Jonathan S. Fisher
Jonathan S. Fisher

Reputation: 8876

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>

Upvotes: 2

Related Questions