How do you change the default locale for a JUnit Test. Current locale is en. I want to test for es_ES. I tried:
System.setProperty("locale", "es_ES");
But this doesn't seem to work.
Java Locale setDefault() method The setDefault() method of Java Locale class is used to set the default locale for this instance of the JVM(Java Virtual Machine). It does not affect the host locale.
In Java, we can use Locale. setDefault() to change the JVM default locale. Alternatively, in the command line, we can configure the user. country and user.
You can set the default Locale
with Locale.setDefault
.
Sets the default locale for this instance of the Java Virtual Machine. This does not affect the host locale.
Locale.setDefault(new Locale("es", "ES"));
Testing with the following resource files:
test.properties
message=Yes
test_es_ES.properties
message=Sí
Here is my main function, with no change to my default locale (en_US):
public static void main(String[] args)
{
ResourceBundle test = ResourceBundle.getBundle("test");
System.out.println(test.getString("message"));
}
Output:
Yes
Here is my main function, with a change to your test locale (es_ES):
public static void main(String[] args)
{
Locale.setDefault(new Locale("es", "ES"));
ResourceBundle test = ResourceBundle.getBundle("test");
System.out.println(test.getString("message"));
}
Output:
Sí
This should work when applied to a JUnit test case.
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