Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/unit-testing/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 比较两个字符串在windows上有效,但在linux上失败_Java_Unit Testing_Character Encoding - Fatal编程技术网

Java 比较两个字符串在windows上有效,但在linux上失败

Java 比较两个字符串在windows上有效,但在linux上失败,java,unit-testing,character-encoding,Java,Unit Testing,Character Encoding,我编写了一个单元测试,它控制德语字母(ä、ö、ß等)是否存在编码问题 此测试在windows pc中通过,但在jenkins(linux环境)上失败。错误消息如下所示 Expected: is "ÄÖÜäöüß" but: was "???????" 我的问题是,将mailText与“ÄÜÜääß”进行比较是错误的吗?我想在比较两个字符串时不需要说明任何编码。可能是您的文件编码和内容在Windows上的定义与在Linux上的不同?听起来好像是文件编码的不同 您可以尝试在.bashrc中显式设置

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

此测试在windows pc中通过,但在jenkins(linux环境)上失败。错误消息如下所示

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

我的问题是,将mailText与“ÄÜÜääß”进行比较是错误的吗?我想在比较两个字符串时不需要说明任何编码。

可能是您的文件编码和内容在Windows上的定义与在Linux上的不同?听起来好像是文件编码的不同

您可以尝试在.bashrc中显式设置编码,或者在Linux中使用
区域设置
-程序(例如UTF-8):

以下内容的输出是什么:

locale

和预期的有什么不同吗


还要检查
defaultCharset()
正在使用的字符集。尝试在两个Windows/Linux上输出该值。

我已按如下方式更改了断言行,并且它工作正常

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

Charset.defaultCharset()
听起来可疑。您能选择UTF-8吗?默认字符集在unix和windows之间可能会有所不同,我希望windows是
ISO-1250(windows)
,但我不知道在unix上应该使用哪个字符集。。。您可以尝试使用UTF-8吗?在您的两个环境中,默认字符集可能不同,因此结果不同。
export LC_ALL=en_US.UTF-8
export LANG=en_US.UTF-8
export LANGUAGE=en_US.UTF-8
assertThat(mailText, is(equalTo(new String("ÄÖÜäöüß".getBytes(defaultCharset)))));