如何更改JUnit测试的默认语言环境。当前语言环境是en。我想测试es_ES。我试过了:
System.setProperty("locale", "es_ES");
但这似乎不起作用。
您可以设置默认Locale
使用Locale.setDefault
。
设置此Java虚拟机实例的默认语言环境。这不会影响主机的语言环境。
Locale.setDefault(new Locale("es", "ES"));
使用以下资源文件进行测试:
test.properties
message=Yes
test_es_ES.properties
message=Sí
这是我的主要功能,没有更改默认语言环境(en_US):
public static void main(String[] args)
{
ResourceBundle test = ResourceBundle.getBundle("test");
System.out.println(test.getString("message"));
}
输出:
Yes
这是我的主要功能,更改了您的测试语言环境(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"));
}
输出:
Sí
将其应用于JUnit测试用例时,该方法应该起作用。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句