java AssertEquals(String, String) 内容相同时比较失败

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/36324452/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 01:16:30  来源:igfitidea点击:

AssertEquals(String, String) ComparisonFailure when contents are identical

javaintellij-ideajunit4

提问by Leonardo

I'm facing the following scenario:

我面临以下情况:

I have an app that spits everything out to the STDOUT (simple company test) and I'm trying to JUnit this.

我有一个应用程序可以将所有内容都输出到 STDOUT(简单的公司测试),我正在尝试对此进行 JUnit。

My problem is, when I run the application, it returns me in the Console: (copy and pasted from IntelliJ)

我的问题是,当我运行应用程序时,它会在控制台中返回我:(从 IntelliJ 复制并粘贴)

Id 1234 nao encontrado
123, R$ 441,00
321, R$ -8490,00
255, R$ 884,00

Print:

打印:

enter image description here

在此处输入图片说明

And my test is:

我的测试是:

assertEquals(outContent.toString().trim(),"Id 1234 nao encontrado\n" +
                "123, R$ 441,00\n" +
                "321, R$ -8490,00\n" +
                "255, R$ 884,00");

I'm getting:

我越来越:

junit.framework.ComparisonFailure:  <Click to see difference>


    at junit.framework.Assert.assertEquals(Assert.java:100)
    at junit.framework.Assert.assertEquals(Assert.java:107)
    at junit.framework.TestCase.assertEquals(TestCase.java:269)
    at com.company.AccountManagerTest.testPrintAccountsBalance(AccountManagerTest.java:85)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)
    at junit.framework.TestCase.runTest(TestCase.java:176)
    at junit.framework.TestCase.runBare(TestCase.java:141)
    at junit.framework.TestResult.protect(TestResult.java:122)
    at junit.framework.TestResult.runProtected(TestResult.java:142)
    at junit.framework.TestResult.run(TestResult.java:125)
    at junit.framework.TestCase.run(TestCase.java:129)
    at junit.framework.TestSuite.runTest(TestSuite.java:252)
    at junit.framework.TestSuite.run(TestSuite.java:247)
    at org.junit.internal.runners.JUnit38ClassRunner.run(JUnit38ClassRunner.java:86)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:69)
    at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:234)
    at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:74)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)

enter image description here

在此处输入图片说明

So, what I am doing wrong here ?

那么,我在这里做错了什么?

Testing with JUnit4 and assertJ 2.4.0

使用 JUnit4 和 assertJ 2.4.0 进行测试

回答by OneCricketeer

The visible characters are identical, but the non-printable characters are not.

可见字符相同,但不可打印字符不同。

You are comparing expected output containing CRLF (\r\n) to actual output with just LF (\n). You can see that in IntelliJ above the right-hand side of both text areas.

您正在将包含 CRLF ( \r\n) 的预期输出与仅包含 LF ( \n) 的实际输出进行比较。您可以在 IntelliJ 中在两个文本区域的右侧上方看到这一点。

Simple solution is to replace the \n's in your string with \r\n. Or remove \rfrom the other.

简单的解决方案是将\n字符串中的's替换为\r\n. 或者\r从另一个中删除。



It is also worth noting that the parameter ordering is actually (Object expected, Object actual), so the outContentshould go second since that is the "actual" output.

还值得注意的是,参数排序实际上是(Object expected, Object actual),因此outContent应该排在第二位,因为那是“实际”输出。

回答by Ulf Lindback

You can use AssertJ "isEqualToNormalizingNewline" as in:

您可以使用 AssertJ "isEqualToNormalizingNewline" 如下:

import static org.assertj.core.api.Assertions.assertThat;

...

@Test
public void ingoreLineEndingCharacterTest() {
    assertThat("First Line\nSecond Line\n").isEqualToNormalizingNewlines("First Line\r\nSecond Line\r\n");
}