2017-02-20 51 views
-1

我已經寫了單元測試,如果有任何編碼問題與德國字母控制(ä,ö,SS等)比較兩個字符串是否在Windows上運行,但無法在Linux上

@Test 
public void testBodyWithDefaultCharset() throws UnsupportedEncodingException { 
    when(backendDefinition.getProperty(BackendDetailsEnum.MAIL_CHARSET.getName())).thenReturn(null); 
    Charset defaultCharset = Charset.defaultCharset(); 
    when(packet.getPayload()).thenReturn(defaultCharset.encode("ÄÖÜäöüß").array()); 

    final String mailText = classUnderTest.prepareMailText(backendDefinition, packet); 

    assertThat(mailText, is(equalTo("ÄÖÜäöüß"))); 
} 

這個測試在Windows PC傳遞,但在jenkins,這是一個Linux環境失敗。錯誤信息如下:

Expected: is "ÄÖÜäöüß" 
but: was "???????" 

我的問題是,這是錯誤的mailText與 「ÄÖÜäöüß」 比較?我想我比較兩個字符串時不需要說明任何編碼。

+2

'Charset.defaultCharset()'聽起來很可疑。你能夠選擇UTF-8嗎? – Ryan

+0

默認字符集可能會在unix和windows之間有所不同,我期望windows是'ISO-1250(Windows)',但我不知道哪一個期望在unix上......你可以試試UTF-8嗎? – Shark

+0

您的兩個環境中的默認字符集可能不一樣,因此會有不同的結果。 –

回答

0

我改變了斷言行如下,它的工作。

assertThat(mailText, is(equalTo(new String("ÄÖÜäöüß".getBytes(defaultCharset))))); 
0

可能是因爲你的文件編碼和東西在Windows上和Linux上定義不同?這聽起來像是文件編碼的區別。

您可以嘗試在.bashrc中明確設定編碼或使用locale -program在Linux下(例如使用UTF-8):

export LC_ALL=en_US.UTF-8 
export LANG=en_US.UTF-8 
export LANGUAGE=en_US.UTF-8 

什麼是輸出:

locale

與預期的不一樣嗎?

另請確保選擇哪一個字符集defaultCharset()正在使用。嘗試在Windows/Linux上輸出值。

相關問題