XmlFixture is the class that helps XML assertions in an easy way.
XmlFixture works using JDom, and it can be created from various type of sources using its static factory methods:
- w3c Document:
After a XmlFixture instance is created, it can be used for XPath matching by calling
This methods will create a
XpathMatch object that contains the XPath match result, with the following method:
node(): This will return the matched node. IMPORTANT: It will return
nullif there is no match found, and throw
IllegalStateExceptionif more than one node matched.
stringValue(): This is the same as node method except that it will return the string value of the matched node.
list: Return the list of matched nodes.
count: Convenience method that returns the number of the matched nodes
hasMatch: Convenience method that returns true if there are matched nodes and false otherwise.
The following code block illustrates how a typical XML assertion looks like with XmlFixture:
XmlFixture xml = XmlFixture.fromText("<person><address type=\"test\">address value</address></person>"); assertEquals("address value", xml.xpath("/person/address[@type='test']")); assertEquals(1, xml.xpathCount("/person/address"));
XmlFixture wraps all the excepion into a
RuntimeException so that you don't have to.
The following are the jars that XmlFixture depends on:
- junit.jar: For backward compatibility. Although you should use JUnit in your project anyway.