Monday, March 29, 2010

XMLUnit: Simpler Asserts

Quite a while ago, I wrote about using XMLUnit in my unit tests to compare XML results.

The method used to create Diffs has been useful, but the project offers a simpler way to compare XML:

import static org.custommonkey.xmlunit.XMLAssert.assertXMLEqual

class AssertXmlEqualTest {

@Test void test_xml_is_similar() {
def expected = "<hi>bye</hi>"
def actual = "<hi>bye</hi>"
assertXMLEqual expected, actual
}
}
The test passes. If I change the value of actual to something different, say:

    def actual = "<hi>goodbye</hi>"

The test fails as the XML documents are no longer similar:

junit.framework.AssertionFailedError: org.custommonkey.xmlunit.Diff
[different] Expected text value 'bye' but was 'goodbye' -
comparing <hi ...>bye<hi> at /hi[1]/text()[1] to <hi ...>goodbye<hi> at /hi[1]/text()[1]
Since I have no reason in this simple case to be inspecting the Diff object created by XMLUnit as described in the previous posting, this method should serve as a nice substitute.

No comments:

Post a Comment