比较两个字符串在Windows上有效,但在Linux上失败 [英] Comparing two strings works on windows but fails on linux

查看:60
本文介绍了比较两个字符串在Windows上有效,但在Linux上失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了一个单元测试,它控制德语字母(ä,ö,ß等)是否存在编码问题。

I have written a unit test, which controls if there are any encoding problems with german letters( ä,ö,ß,etc.)

@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环境。错误消息如下;

This test passes in windows pc but fails on jenkins, which is a linux environment. The error message is as follows;

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

我的问题是,将mailText与ÄÖÜäööß进行比较是否错误?比较两个字符串时,我不需要声明任何编码。

My question is, is it wrong to compare mailText with "ÄÖÜäöüß"? I thougt I don't need to state any encoding when I compare two strings.

推荐答案

我将声明行更改如下

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

这篇关于比较两个字符串在Windows上有效,但在Linux上失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆