Skip to content

Instantly share code, notes, and snippets.

@mcs
Created October 19, 2011 06:48
Show Gist options
  • Save mcs/1297630 to your computer and use it in GitHub Desktop.
Save mcs/1297630 to your computer and use it in GitHub Desktop.
Using XMLUnit in JUnit4 to test XML
package learning_tests;
import static org.custommonkey.xmlunit.XMLAssert.*;
import org.custommonkey.xmlunit.XMLUnit;
import org.junit.Test;
public class XmlUnitTest {
@Test
public void compareIdenticalXmlStrings() throws Exception {
String xml1 = "<root><node>Test</node></root>";
String xml2 = "<root><node>Test</node></root>";
assertXMLEqual(xml1, xml2);
}
@Test
public void compareSimilarXmlStringsWithoutTolerance() throws Exception {
String xml1 = "<root><node>Test</node></root>";
String xml2 = "<root>\n <node>Test</node>\n</root>";
assertXMLNotEqual(xml1, xml2);
}
@Test
public void compareSimilarXmlStringsIgnoringWhitespace() throws Exception {
String xml1 = "<root><node>Test</node></root>";
String xml2 = "<root>\n <node> Test </node>\n</root>";
XMLUnit.setIgnoreWhitespace(true);
assertXMLEqual(xml1, xml2);
}
@Test
public void compareDifferentXmlStringsIgnoringWhitespace() throws Exception {
String xml1 = "<root><node>Test</node></root>";
String xml2 = "<root><node>Te st</node></root>";
XMLUnit.setIgnoreWhitespace(true);
assertXMLNotEqual(xml1, xml2);
}
}
@samiron
Copy link

samiron commented Dec 5, 2012

Hello mcs, your code really helped me. Thanks

@szydan
Copy link

szydan commented Jun 10, 2013

Thanks import static org.custommonkey.xmlunit.XMLAssert.* was what I needed :-)

@kyxap
Copy link

kyxap commented Sep 24, 2013

Hello, it's ok that the compareSimilarXmlStringsWithoutTolerance() if failed ?

thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment