21

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.

My problem is, when I run the application, it returns me in the Console: (copy and pasted from 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$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)

enter image description here

So, what I am doing wrong here ?

Testing with JUnit4 and assertJ 2.4.0

serv-inc
  • 35,772
  • 9
  • 166
  • 188
Leonardo
  • 3,141
  • 3
  • 31
  • 60

3 Answers3

33

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.

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


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

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
  • 7
    If you're not sure whether the string will have `\r` or not, another solution is to use `outContent.toString().trim().replace("\r","")` as the actual string. That is, remove all the `\r` characters before comparing. – ajb Mar 31 '16 at 05:45
  • Any way to debug this except by looking at it by hand? – serv-inc Jun 07 '17 at 16:31
  • There are 31 non printable characters, so I would say that if you really don't care about them, then just strip them all out. https://stackoverflow.com/questions/6198986/how-can-i-replace-non-printable-unicode-characters-in-java – OneCricketeer Jun 07 '17 at 16:57
11

You can use AssertJ "isEqualToNormalizingNewline" as in:

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");
}
Ulf Lindback
  • 13,974
  • 3
  • 40
  • 31
1

The real issue is with System configuration of "line separator", not with unit test or input / output. Likely code is run on Windows platform which has default line separator as CRLF or \r\n. Unix or Mac, however, have default line separator as LF or \n.

Simplest way to fix this could be setting default line separator in the IDE. For example, IntelliJ provides elegant way of fixing this IDE or Project wide, or for a file. See IntelliJ : Configuring Line Separators.

IDE / Project wide

File

sbharti
  • 899
  • 11
  • 22