Chapter 9. Testing and XML
This chapter covers
- Comparing XML documents with XMLUnit
- Ignoring superficial differences in XML documents
- Testing static web pages with XMLUnit
- Testing XSL transformations with XMLUnit
- Validating XML documents during testing
XML documents are everywhere in J2EE: deployment descriptors, configuration files, XHTML and XSL transformations, Web Services Description Language, and on and on. If you are going to test J2EE applications, you are going to need to write tests involving XML documents. Notice the title of this chapter is “Testing and XML,” and not “Testing XML.” It is not our mission here to test XML parsers, but rather to describe how to write tests involving XML documents and other XML-related technologies, such as XSL transformations. Given that XML documents are everywhere, it looks to us that this is an important skill to have, and we can summarize the entire chapter in two words: use XPath.
The XPath language defines a way to refer to the content of an XML document with query expressions that can locate anything from single XML attribute values to large complex collections of XML elements. If you have not seen XPath before, we recommend Elizabeth Castro’s XML for the World Wide Web, which describes XML, XSL, and XPath in detail.[1] The overall strategy when testing with XML documents is to make assertions about those documents using XPath to retrieve the actual content. You want to write assertions like this: