2015-04-03 63 views
0

注意:我現在使用Google的設置recommends來在Android Studio中執行JUnit測試。Android Studio中的JUnit未正確使用貨幣格式

我正在試圖對存儲貨幣值爲double的代碼段進行單元測試,並以該貨幣的格式輸出字符串。我用java.text.NumberFormat做到這一點:

public NumberFormat viewFormat; 
public NumberFormat editFormat; 

public CurrencyField(String languageCode, String countryCode) { 
    viewFormat = NumberFormat.getCurrencyInstance(new Locale(languageCode, countryCode)); 
    viewFormat.setRoundingMode(RoundingMode.FLOOR); 

這裏就是我得到了我想要的值進行格式化顯示:

public String getDisplayValue() 
{ 
    double numberValue = (double) mValue.getValue(); 
    return mField.viewFormat.format(numberValue); 
} 

當我運行代碼的Android應用程序的一部分,號碼是格式正確。例如,如果我使用的是以色列謝克爾爲我的貨幣,我分別記

Field currencyField = new CurrencyField("he", "ISR"); 

其中"he""ISR"是希伯來語和以色列的代碼。如果該號碼在內部存儲爲雙重45.12,則會以字符串"45.12 ₪"輸出,這是正確的。然而,當我嘗試使用JUnit運行相同的代碼,下面是測試輸出:

org.junit.ComparisonFailure: 
Expected :45.12 ₪ 
Actual :¤ 45.12 

這在我看來,JUnit的或Android Studio的測試過程中由於某種原因丟失了一些Java庫。我甚至無法以編程方式獲得希克爾符號的預期值。我試圖用java.util.Currency,並沒有工作,然後我試圖

expectedDisplayValue += ((CurrencyField) field).viewFormat.getCurrency().getSymbol(new Locale("he", "ISR")); 

而且也不能工作。我必須明確定義

private String expectedDisplayValue = "45.12 ₪"; 

爲什麼格式化在運行時工作,但不在測試中?

+0

我猜測,語言環境沒有在JUnit中設置。 – chrylis 2015-04-04 00:01:21

回答

0

事實證明,我使用的是錯誤的ISO 3166-1國家代碼。我使用的是alpha 3代碼(「ISR」),而不是alpha 2代碼(「IL」),它在應用程序中工作,但不在測試中。當我使用希伯來語時,測試仍然給我錯誤的符號和格式,但我會爲此提出一個單獨的問題。與此同時,我會使用其他一些外幣進行測試。