public class XmlAssert extends org.assertj.core.api.AbstractAssert<XmlAssert,Object>
All types which are supported by Input.from(Object)
can be used as input for assertThat(Object)
Simple Example
import static org.xmlunit.assertj.XmlAssert.assertThat; final String xml = "<a><b attr=\"abc\"></b></a>"; assertThat(xml).nodesByXPath("//a/b/@attr").exist(); assertThat(xml).hasXPath("//a/b/@attr"); assertThat(xml).doesNotHaveXPath("//a/b/c");
Example with namespace mapping
String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + "<feed xmlns=\"http://www.w3.org/2005/Atom\">" + " <title>title</title>" + " <entry>" + " <title>title1</title>" + " <id>id1</id>" + " </entry>" + "</feed>"; HashMap<String, String> prefix2Uri = new HashMap<String, String>(); prefix2Uri.put("atom", "http://www.w3.org/2005/Atom"); assertThat(xml) .withNamespaceContext(prefix2Uri) .hasXPath("//atom:feed/atom:entry/atom:id"));
Testing XPath expression value
String xml = "<a><b attr=\"abc\"></b></a>"; assertThat(xml).valueByXPath("//a/b/@attr").isEqualTo("abc"); assertThat(xml).valueByXPath("count(//a/b)").isEqualTo(1);
Example with XML validation
String xml = "<a><b attr=\"abc\"></b></a>"; StreamSource xsd = new StreamSource(new File("schema.xsd")); assertThat(xml).isValid(); assertThat(xml).isValidAgainst(xsd);
Example with XMLs comparision
final String control = "<a><b attr=\"abc\"></b></a>"; final String test = "<a><b attr=\"xyz\"></b></a>"; assertThat(test).and(control).areIdentical(); assertThat(test).and(control).areNotIdentical(); assertThat(test).and(control).areSimilar(); assertThat(test).and(control).areNotSimilar(); assertThat(test).and(control) .normalizeWhitespace() .ignoreComments() .withNodeMatcher(new DefaultNodeMatcher(new MyElementSelector())) .withDifferenceEvaluator(DifferenceEvaluators.chain( DifferenceEvaluators.Default, new MyDifferenceEvaluator())); .areIdentical();
Modifier and Type | Method and Description |
---|---|
CompareAssert |
and(Object control)
Create
CompareAssert for given control XML source and actual XML source. |
static XmlAssert |
assertThat(Object o)
Factory method for
XmlAssert |
void |
doesNotHaveXPath(String xPath)
Equivalent for
|
MultipleNodeAssert |
hasXPath(String xPath)
Equivalent for
|
XmlAssert |
isInvalid()
Check if actual value is not valid against W3C XML Schema
|
XmlAssert |
isNotValidAgainst(Object... schemaSources)
Check if actual value is not valid against schema provided by given sources
|
XmlAssert |
isNotValidAgainst(Schema schema)
Check if actual value is not valid against given schema
|
XmlAssert |
isValid()
Check if actual value is valid against W3C XML Schema
|
XmlAssert |
isValidAgainst(Object... schemaSources)
Check if actual value is valid against schema provided by given sources
|
XmlAssert |
isValidAgainst(Schema schema)
Check if actual value is valid against given schema
|
MultipleNodeAssert |
nodesByXPath(String xPath)
Create
MultipleNodeAssert from nodes selecting by given xPath. |
ValueAssert |
valueByXPath(String xPath)
Create
ValueAssert from value of given xPath expression. |
XmlAssert |
withDocumentBuilderFactory(DocumentBuilderFactory dbf)
Sets the
DocumentBuilderFactory to use when creating a
Document from the XML input. |
XmlAssert |
withNamespaceContext(Map<String,String> prefix2Uri)
Utility method used for creating a namespace context mapping to be used in XPath matching.
|
XmlAssert |
withXPathFactory(XPathFactory xpf)
Sets the
XPathFactory to use for XPath related assertions. |
as, as, asList, asString, describedAs, describedAs, descriptionText, doesNotHave, doesNotHaveSameClassAs, equals, failWithMessage, getWritableAssertionInfo, has, hashCode, hasSameClassAs, hasSameHashCodeAs, hasToString, inBinary, inHexadecimal, is, isEqualTo, isExactlyInstanceOf, isIn, isIn, isInstanceOf, isInstanceOfAny, isNot, isNotEqualTo, isNotExactlyInstanceOf, isNotIn, isNotIn, isNotInstanceOf, isNotInstanceOfAny, isNotNull, isNotOfAnyClassIn, isNotSameAs, isNull, isOfAnyClassIn, isSameAs, overridingErrorMessage, setCustomRepresentation, throwAssertionError, usingComparator, usingDefaultComparator, withFailMessage, withRepresentation, withThreadDumpOnError
public static XmlAssert assertThat(Object o)
XmlAssert
o
- object with type supported by Input.from(Object)
public XmlAssert withDocumentBuilderFactory(DocumentBuilderFactory dbf)
DocumentBuilderFactory
to use when creating a
Document
from the XML input.AssertionError
- if the actual value is null
.public XmlAssert withXPathFactory(XPathFactory xpf)
XPathFactory
to use for XPath related assertions.AssertionError
- if the actual value is null
.public XmlAssert withNamespaceContext(Map<String,String> prefix2Uri)
prefix2Uri
- prefix2Uri maps from prefix to namespace URI. It is used to resolve
XML namespace prefixes in the XPath expressionAssertionError
- if the actual value is null
.public MultipleNodeAssert nodesByXPath(String xPath)
MultipleNodeAssert
from nodes selecting by given xPath.AssertionError
- if the xPath is blank.AssertionError
- if the actual value is null
.AssertionError
- if the actual value provide invalid XML.public MultipleNodeAssert hasXPath(String xPath)
nodesByXPath(xPath)
.exist()
public void doesNotHaveXPath(String xPath)
nodesByXPath(xPath)
.doNotExist()
public ValueAssert valueByXPath(String xPath)
ValueAssert
from value of given xPath expression.AssertionError
- if the xPath is blank.AssertionError
- if the actual value is null
.AssertionError
- if the actual value provide invalid XML.public CompareAssert and(Object control)
CompareAssert
for given control XML source and actual XML source.AssertionError
- if the actual value is null
AssertionError
- if the control value is null
public XmlAssert isValid()
AssertionError
- if the actual value is null
.AssertionError
- if the actual value is invalidpublic XmlAssert isInvalid()
AssertionError
- if the actual value is null
.AssertionError
- if the actual value is validpublic XmlAssert isValidAgainst(Schema schema)
AssertionError
- if the actual value is null
.AssertionError
- if the actual value is invalidpublic XmlAssert isNotValidAgainst(Schema schema)
AssertionError
- if the actual value is null
.AssertionError
- if the actual value is validpublic XmlAssert isValidAgainst(Object... schemaSources)
AssertionError
- if the actual value is null
.AssertionError
- if the actual value is invalidpublic XmlAssert isNotValidAgainst(Object... schemaSources)
AssertionError
- if the actual value is null
.AssertionError
- if the actual value is validCopyright © 2001–2020 XMLUnit. All rights reserved.