Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Comparing XML elements in JUnit

Tags:

junit

xml

JUnit class which tries to compare the XML input with the XML output fails.

TestCase.assertEquals(msg, expected, actual);

Is there any way to compare XML i/p & o/p from JUnit?

like image 743
Aravind Avatar asked Nov 07 '11 07:11

Aravind


Video Answer


1 Answers

You could look at XMLUnit. This allows you to compare two XML documents. From the docs:

XMLUnit enables JUnit-style assertions to be made about the content and structure of XML. It is an open source project hosted at http://xmlunit.sourceforge.net/ that grew out of a need to test a system that generated and received custom XML messages. The problem that we faced was how to verify that the system generated the correct message from a known set of inputs. Obviously we could use a DTD or a schema to validate the message output, but this approach wouldn't allow us to distinguish between valid XML with correct content (e.g. element bar) and valid XML with incorrect content (e.g. element baz). What we really wanted was an assertXMLEqual() method, so we could compare the message that we expected the system to generate and the message that the system actually generated.

like image 172
Matthew Farwell Avatar answered Sep 30 '22 19:09

Matthew Farwell