AssertEquals(String,String)ComparisonFailure当内容相同时

Leo*_*rdo 11 java intellij-idea junit4

我面临以下情况:

我有一个应用程序,将所有东西吐出到STDOUT(简单的公司测试),我正在尝试JUnit.

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

Id 1234 nao encontrado
123, R$ 441,00
321, R$ -8490,00
255, R$ 884,00
Run Code Online (Sandbox Code Playgroud)

打印:

在此输入图像描述

我的测试是:

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

我越来越:

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$1.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)
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

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

使用JUnit4和assertJ 2.4.0进行测试

cri*_*007 23

可见字符相同,但不可打印的字符不相同.

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

简单的解决方案是用\n你的字符串替换你的字符串\r\n.或者\r从另一个中删除.


值得注意的是参数排序实际上是(Object expected, Object actual),因此outContent应该是第二,因为那是"实际"输出.

  • 如果你不确定该字符串是否有`\ r \n',另一个解决方案是使用`outContent.toString().trim().replace("\ r","")`作为实际的字符串.也就是说,在比较之前删除所有`\ r`字符. (4认同)

Ulf*_*ack 8

您可以使用 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");
}
Run Code Online (Sandbox Code Playgroud)