0

I have a bit of a weird issue where I can build our code base without any issues via TeamCity. On the other hand when I trigger the build via Eclipse I get org.junit.ComparisonFailure.

Code that is failing is as follows:

@Test
public void prettyPrintTest() throws BunchOfExceptions {
    InputStream formattedXml = DomUtilsTest.class.getResourceAsStream("/path/to/sample/file/formattedOutput.xml");
    InputStream notFormattedXml = DomUtilsTest.class.getResourceAsStream("/path/to/sample/file/notFormattedInput.xml");

    DocumentBuilder builder = ClassBeingTested.getDocumentBuilderFactory().newDocumentBuilder();
    Document notFormattedDocument = builder.parse(notFormattedXml);
    String prettyPrintedXmlContent = ClassBeingTested.prettyPrint(notFormattedDocument);
    
    Assert.assertEquals(IOUtils.toString(formattedXml), prettyPrintedXmlContent);
}

The unit test is fine on TeamCity, and it is fine on my colleague's IntelliJ (he wrote this code). But I get the following error on my machine:

org.junit.ComparisonFailure: 
expected:<<message>[
    <header>
        <messagetype>HelloWorld</messagetype>
    </header>
    <body>
        <messageBody>HolaComoEstas!</messageBody>
    </body>
</message>
]
> but was:<<message>[
    <header>
        <messagetype>HelloWorld</messagetype>
    </header>
    <body>
        <messageBody>HolaComoEstas!</messageBody>
    </body>
</message>
] 

Any idea how I could configure Eclipse to run this in the same way that a TeamCity Unix host does it? Or is there a way to make this unit test platform independent?

Muhamad Gafar
  • 409
  • 3
  • 12
  • 2
    Are you on Windows, and are you using Git? Your problem is likely one of line-endings (`\n` on Linux, `\r\n` on Windows). You may need to normalize it, or use a diff/comparison library that is able to ignore such differences. – Mark Rotteveel Jun 24 '21 at 14:03
  • Probably encoding problem. What if you manually compare the two strings? – Thorbjørn Ravn Andersen Jun 24 '21 at 14:04
  • Also consider comparing XML properly instead of as strings. – Thorbjørn Ravn Andersen Jun 24 '21 at 14:05
  • @ThorbjørnRavnAndersen it has to be compared as strings because the pretty print function is used in a logger as a string. When I manually compare the strings they look identical. – Muhamad Gafar Jun 24 '21 at 14:20
  • Thanks @MarkRotteveel, I ran dos2unix to account for line-endings and that fixed my issue. See answer I submitted. – Muhamad Gafar Jun 24 '21 at 14:24
  • 1
    If you're using git, you can also configure it to not change the lineendings, see [How do I force git to use LF instead of CR+LF under windows?](https://stackoverflow.com/questions/2517190/how-do-i-force-git-to-use-lf-instead-of-crlf-under-windows) – Mark Rotteveel Jun 24 '21 at 14:39

1 Answers1

0

I was able to fix this by opening git bash, going to the path of the formatted XML file (E.g. C:/path/to/sample/file/formattedOutput.xml) then executing dos2unix as:

dos2unix.exe ./formattedOutput.xml

So it looks like Mark Rotteveel was correct in the comment above.

Thanks

Muhamad Gafar
  • 409
  • 3
  • 12