java - AssertEquals(String, String) ComparisonFailure when contents are identical

ID : 274366

viewed : 26

Tags : javaintellij-ideajunit4java





Top 4 Answer for java - AssertEquals(String, String) ComparisonFailure when contents are identical

vote vote

98

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.

vote vote

86

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"); } 
vote vote

77

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

vote vote

61

Top 3 video Explaining java - AssertEquals(String, String) ComparisonFailure when contents are identical







Related QUESTION?