7
如何更改JUnit測試的默認語言環境。當前區域設置是en。我想測試es_ES。 我想:更改junit測試的默認語言環境
System.setProperty("locale", "es_ES");
但這似乎並沒有工作。
如何更改JUnit測試的默認語言環境。當前區域設置是en。我想測試es_ES。 我想:更改junit測試的默認語言環境
System.setProperty("locale", "es_ES");
但這似乎並沒有工作。
您可以使用Locale.setDefault
來設置默認Locale
。
設置此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測試用例。
是的,?你運行的是什麼代碼是語言環境相關的? – fge 2014-12-05 22:03:45
我沒有明確你的意思。我有一些需要檢查的異常消息。我已經創建了一個單獨的資源文件,但是,它沒有鏈接.. – user3453784 2014-12-05 22:19:09
Hmwell,也許在這種情況下,你想看看[這個項目](https://github.com/fge/msg-simple) ;它可以讓你很容易地獲得本地化的消息 – fge 2014-12-05 22:23:09