如何更改 JUnit 测试的默认语言环境。当前语言环境是 en。我想测试 es_ES。 我试过了:
System.setProperty("locale", "es_ES");
但这似乎行不通。
最佳答案
您可以使用 Locale.setDefault
设置默认的 Locale
.
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"));
使用以下资源文件进行测试:
测试.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 测试用例。
关于java - 更改 junit 测试的默认语言环境,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27325382/