-2

I'm trying to compare a String to a file-content, and want to assert wheather they are equal or not.

Like:

13CB0000000000X15.xml (input)
13CB0000000000X15.xml (expected (content of file))

My Assert:

assertTrue("Tatsaechliches Ergebnis weicht von '" 
+ expectedResultFileName + "' ab!", result.equals(expected));

This assertion somehow fails, because it says its not equal?!

My file is UTF-8 w/o BOM. There is no LF/ CRLF or else. I even tried to trim it with:

    String expected = readFile(testDataBasePath + 
    File.separator + expectedResultFileName, encoding);
    expected = expected.trim();

    String result   = document.getDocumentElement().getChildNodes().item(0).getNodeValue();             
    result = result.trim();

What am I missing?

0x45
  • 31
  • 1
  • 9
  • So you never checked the bytes contained in these String and which bytes are missing/surplus? – Tom Dec 14 '16 at 13:21
  • 5
    Hint: when equals() tells you that your strings aren't equal; then they arent. Try to compare them character wise for example. – GhostCat Dec 14 '16 at 13:22
  • Thanks @GhostCat, got it solved with ur methode. For the record: `char[] first = w1.toLowerCase().toCharArray(); char[] second = w2.toLowerCase().toCharArray(); int minLength = Math.min(first.length, second.length); for(int i = 0; i < minLength; i++) { if (first[i] != second[i]) { return false; } } return true;` – 0x45 Dec 14 '16 at 13:30
  • @HendrikHeim Better tell us which character were different. :) – lexicore Dec 14 '16 at 13:34
  • @lexicore How is that important? This question is a "why doesn't this code work" question without a proper mcve and even if this will be fixed, what value does this have for future readers? I would vote to delete, if I could. – Tom Dec 14 '16 at 13:36
  • @Tom I agree that absent MCVE is a close reason. I disagree that the question can't be helpful for future readers. The core of the question is "how can equally-looking strings be not equal" and it's not bad to know possible reasons for this. For the same reason I'm interested in the particular problem which was the case here. – lexicore Dec 14 '16 at 13:43
  • @lexicore *"how can equally-looking strings be not equal"* There are other questions for this already. I'm currently looking for a good dupe to close vote this one here. – Tom Dec 14 '16 at 13:48
  • Arr, way too many questions about general String comparison, but anyway this is a quite useful dupe: http://stackoverflow.com/questions/37457545/strings-contain-the-same-characters-but-are-still-different. Although it asks about the BOM and OP said it isn't a problem in his case, the answers contain general ways to analyze two Strings, like checking the characters or bytes. – Tom Dec 14 '16 at 14:09
  • 1
    @HendrikHeim Your solution in the comment would be wrong for `w1 = "foo"; and w2 = "foobar"`. Either provide an example which demonstrate that it's failing like you said or delete/close your question. – SubOptimal Dec 14 '16 at 15:28

1 Answers1

0

Your string are not equal, period. They might look equal when you print them out but there's a lot of tricks like different types of space characters, all looking the same. Or as simple as mixing lowecase l and uppercase I which are almost indistinguishable with some fonts.

One hint: consider using assertEquals(..., a, b) instead of assertTrue(..., a.equals(b)). The former may give you a better message in some IDEs, showing exactly where strings differ.

lexicore
  • 42,748
  • 17
  • 132
  • 221