Package org.xmlunit.assertj
Class XmlAssert
- java.lang.Object
-
- org.assertj.core.api.AbstractAssert<XmlAssert,java.lang.Object>
-
- org.xmlunit.assertj.XmlAssert
-
- All Implemented Interfaces:
org.assertj.core.api.Assert<XmlAssert,java.lang.Object>
,org.assertj.core.api.Descriptable<XmlAssert>
,org.assertj.core.api.ExtensionPoints<XmlAssert,java.lang.Object>
public class XmlAssert extends org.assertj.core.api.AbstractAssert<XmlAssert,java.lang.Object>
Entry point for fluent interface for writing assertions based on AssertJ library.All types which are supported by
Input.from(Object)
can be used as input forassertThat(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();
- Since:
- XMLUnit 2.6.1
-
-
Field Summary
Fields Modifier and Type Field Description private javax.xml.parsers.DocumentBuilderFactory
dbf
private java.util.Map<java.lang.String,java.lang.String>
prefix2Uri
private javax.xml.xpath.XPathFactory
xpf
-
Constructor Summary
Constructors Modifier Constructor Description private
XmlAssert(java.lang.Object o)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description CompareAssert
and(java.lang.Object control)
CreateCompareAssert
for given control XML source and actual XML source.static XmlAssert
assertThat(java.lang.Object o)
Factory method forXmlAssert
void
doesNotHaveXPath(java.lang.String xPath)
Equivalent forMultipleNodeAssert
hasXPath(java.lang.String xPath)
Equivalent forXmlAssert
isInvalid()
Check if actual value is not valid against W3C XML SchemaXmlAssert
isNotValidAgainst(java.lang.Object... schemaSources)
Check if actual value is not valid against schema provided by given sourcesXmlAssert
isNotValidAgainst(javax.xml.validation.Schema schema)
Check if actual value is not valid against given schemaXmlAssert
isValid()
Check if actual value is valid against W3C XML SchemaXmlAssert
isValidAgainst(java.lang.Object... schemaSources)
Check if actual value is valid against schema provided by given sourcesXmlAssert
isValidAgainst(javax.xml.validation.Schema schema)
Check if actual value is valid against given schemaMultipleNodeAssert
nodesByXPath(java.lang.String xPath)
CreateMultipleNodeAssert
from nodes selecting by given xPath.ValueAssert
valueByXPath(java.lang.String xPath)
CreateValueAssert
from value of given xPath expression.XmlAssert
withDocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory dbf)
Sets theDocumentBuilderFactory
to use when creating aDocument
from the XML input.XmlAssert
withNamespaceContext(java.util.Map<java.lang.String,java.lang.String> prefix2Uri)
Utility method used for creating a namespace context mapping to be used in XPath matching.XmlAssert
withXPathFactory(javax.xml.xpath.XPathFactory xpf)
Sets theXPathFactory
to use for XPath related assertions.-
Methods inherited from class org.assertj.core.api.AbstractAssert
as, as, asInstanceOf, asList, asString, describedAs, describedAs, descriptionText, doesNotHave, doesNotHaveSameClassAs, equals, failWithMessage, getWritableAssertionInfo, has, hashCode, hasSameClassAs, hasSameHashCodeAs, hasToString, inBinary, inHexadecimal, is, isEqualTo, isExactlyInstanceOf, isIn, isIn, isInstanceOf, isInstanceOfAny, isInstanceOfSatisfying, isNot, isNotEqualTo, isNotExactlyInstanceOf, isNotIn, isNotIn, isNotInstanceOf, isNotInstanceOfAny, isNotNull, isNotOfAnyClassIn, isNotSameAs, isNull, isOfAnyClassIn, isSameAs, matches, matches, newListAssertInstance, overridingErrorMessage, satisfies, satisfies, satisfiesAnyOf, satisfiesAnyOf, setCustomRepresentation, throwAssertionError, usingComparator, usingComparator, usingDefaultComparator, withFailMessage, withRepresentation, withThreadDumpOnError
-
-
-
-
Method Detail
-
assertThat
public static XmlAssert assertThat(java.lang.Object o)
Factory method forXmlAssert
- Parameters:
o
- object with type supported byInput.from(Object)
-
withDocumentBuilderFactory
public XmlAssert withDocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory dbf)
Sets theDocumentBuilderFactory
to use when creating aDocument
from the XML input.- Throws:
java.lang.AssertionError
- if the actual value isnull
.
-
withXPathFactory
public XmlAssert withXPathFactory(javax.xml.xpath.XPathFactory xpf)
Sets theXPathFactory
to use for XPath related assertions.- Throws:
java.lang.AssertionError
- if the actual value isnull
.
-
withNamespaceContext
public XmlAssert withNamespaceContext(java.util.Map<java.lang.String,java.lang.String> prefix2Uri)
Utility method used for creating a namespace context mapping to be used in XPath matching.- Parameters:
prefix2Uri
- prefix2Uri maps from prefix to namespace URI. It is used to resolve XML namespace prefixes in the XPath expression- Throws:
java.lang.AssertionError
- if the actual value isnull
.
-
nodesByXPath
public MultipleNodeAssert nodesByXPath(java.lang.String xPath)
CreateMultipleNodeAssert
from nodes selecting by given xPath.- Throws:
java.lang.AssertionError
- if the xPath is blank.java.lang.AssertionError
- if the actual value isnull
.java.lang.AssertionError
- if the actual value provide invalid XML.
-
hasXPath
public MultipleNodeAssert hasXPath(java.lang.String xPath)
Equivalent fornodesByXPath(xPath)
.exist()
-
doesNotHaveXPath
public void doesNotHaveXPath(java.lang.String xPath)
Equivalent fornodesByXPath(xPath)
.doNotExist()
-
valueByXPath
public ValueAssert valueByXPath(java.lang.String xPath)
CreateValueAssert
from value of given xPath expression.- Throws:
java.lang.AssertionError
- if the xPath is blank.java.lang.AssertionError
- if the actual value isnull
.java.lang.AssertionError
- if the actual value provide invalid XML.
-
and
public CompareAssert and(java.lang.Object control)
CreateCompareAssert
for given control XML source and actual XML source.- Throws:
java.lang.AssertionError
- if the actual value isnull
java.lang.AssertionError
- if the control value isnull
-
isValid
public XmlAssert isValid()
Check if actual value is valid against W3C XML Schema- Throws:
java.lang.AssertionError
- if the actual value isnull
.java.lang.AssertionError
- if the actual value is invalid
-
isInvalid
public XmlAssert isInvalid()
Check if actual value is not valid against W3C XML Schema- Throws:
java.lang.AssertionError
- if the actual value isnull
.java.lang.AssertionError
- if the actual value is valid
-
isValidAgainst
public XmlAssert isValidAgainst(javax.xml.validation.Schema schema)
Check if actual value is valid against given schema- Throws:
java.lang.AssertionError
- if the actual value isnull
.java.lang.AssertionError
- if the actual value is invalid
-
isNotValidAgainst
public XmlAssert isNotValidAgainst(javax.xml.validation.Schema schema)
Check if actual value is not valid against given schema- Throws:
java.lang.AssertionError
- if the actual value isnull
.java.lang.AssertionError
- if the actual value is valid
-
isValidAgainst
public XmlAssert isValidAgainst(java.lang.Object... schemaSources)
Check if actual value is valid against schema provided by given sources- Throws:
java.lang.AssertionError
- if the actual value isnull
.java.lang.AssertionError
- if the actual value is invalid
-
isNotValidAgainst
public XmlAssert isNotValidAgainst(java.lang.Object... schemaSources)
Check if actual value is not valid against schema provided by given sources- Throws:
java.lang.AssertionError
- if the actual value isnull
.java.lang.AssertionError
- if the actual value is valid
-
-