java 更改 junit 测试的默认语言环境
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27325382/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
change default locale for junit test
提问by user3453784
How do you change the default locale for a JUnit Test. Current locale is en. I want to test for es_ES. I tried:
您如何更改 JUnit 测试的默认语言环境。当前语言环境是 en。我想测试 es_ES。我试过:
System.setProperty("locale", "es_ES");
But this doesn't seem to work.
但这似乎不起作用。
回答by rgettman
You can set the default Locale
with Locale.setDefault
.
您可以设置默认Locale
使用Locale.setDefault
。
Sets the default locale for this instance of the Java Virtual Machine. This does not affect the host locale.
设置此 Java 虚拟机实例的默认语言环境。这不会影响主机语言环境。
Locale.setDefault(new Locale("es", "ES"));
Testing with the following resource files:
使用以下资源文件进行测试:
test.properties
message=Yes
test_es_ES.properties
message=Sí
测试属性
message=Yes
test_es_ES.properties
message=Sí
Here is my main function, with no change to my default locale (en_US):
这是我的主要功能,我的默认语言环境(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):
这是我的主要功能,更改了您的测试区域设置 (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.
这在应用于 JUnit 测试用例时应该有效。