0

I have converted a string to an XML document using the code below:

String xmlStr = "<msg><uuid>12345</uuid></msg>"
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder;
try {
  builder = factory.newDocumentBuilder();
  Document doc = builder.parse(new InputSource(new StringReader(xmlStr)));
  return doc;
} catch (Exception e) {
  throw new RuntimeException(e);
}

Then I converted an XML file to a document with the following:

File file = new File("src/test/resources/xmlForJunitTest.xml");
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document expectedDoc = db.parse(file);

Finally I compare the two documents:

Document actualDoc = XmlUtils.convertStringToDocument(xmlString);
Diff myDiff = new Diff(expectedDoc, actualDoc);
assert (myDiff.similar());

This test passes using an XML file (xmlForJunitTest.xml) formatted like so:

<msg><uuid>12345</uuid></msg>

And it fails with this:

<msg>
   <uuid>12345</uuid>
</msg>

Please you can suggest why this failure occurs, and what the solution is?

Ben
  • 7,548
  • 31
  • 45
Rajeev
  • 519
  • 3
  • 13
  • 29

1 Answers1

0

The assertion fails because one document includes whitespace, and the other doesn't. I believe you need to look at the normalizeWhitespace flag in XmlUnit (assuming that's what you're using).

Ben
  • 7,548
  • 31
  • 45