2

I am working on a Junit test where the output of a method is a String in XML format.I am not sure why the string comparison is failing

Assert.assertEquals("<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\n" +
            "<RATE>1.0</RATE>", XMLUtil.formatXML(TEST_XML));


Actaul response from the method :
 <?xml version="1.0" encoding="ISO-8859-1"?>
<RATE>1.0</RATE>

Error :

junit.framework.ComparisonFailure: expected:<...oding="ISO-8859-1"?>[
<RATE>1.0</RATE>]> but was:<...oding="ISO-8859-1"?>[
<RATE>1.0</RATE>
]>
    ]

EDIT Even adding new line in the end doesnt help:

Assert.assertEquals("<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\n" +
                "<RATE>1.0</RATE>\n", XMLUtil.formatXML(TEST_XML));

junit.framework.ComparisonFailure: expected:<...oding="ISO-8859-1"?>[
<RATE>1.0</RATE>]
> but was:<...oding="ISO-8859-1"?>[
<RATE>1.0</RATE>
]
>
7
  • 4
    Different or missing line ending would be an obvious possibility Commented Jul 18, 2013 at 13:17
  • 2
    The actual response has a new line at the end of the XML. Side note: this looks like it would be somehwat unreliable. Commented Jul 18, 2013 at 13:18
  • how can i create a string with the correct line ending ? Commented Jul 18, 2013 at 13:19
  • use trim() to remove the line endings Commented Jul 18, 2013 at 13:20
  • 1
    Consider using XMLUnit for the job. Many things (not just line-spacing) may differ in XML while still making two files similar. Don't reinvent the wheel. Commented Jul 18, 2013 at 13:27

1 Answer 1

2

The expected and actual XML may have different carriage control character content. Try removing all newline and linefeed characters

String expected = 
       "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>RATE>1.0</RATE>";
Assert.assertEquals(expected,
       "<RATE>1.0</RATE>", XMLUtil.formatXML(TEST_XML).replaceAll("[\r\n]", "");
Sign up to request clarification or add additional context in comments.

2 Comments

i'd remove tabs too. actually i'd consider removing all whitespace ie \\s
That depends. The test may need to test for exact whitespace content.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.