Package org.xmlunit.assertj3
Class XmlAssert
- All Implemented Interfaces:
org.assertj.core.api.Assert<XmlAssert,,Object> org.assertj.core.api.Descriptable<XmlAssert>,org.assertj.core.api.ExtensionPoints<XmlAssert,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 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();
- Since:
- XMLUnit 2.8.1
-
Field Summary
Fields inherited from class org.assertj.core.api.AbstractAssert
actual, info, myself, objects, throwUnsupportedExceptionOnEquals -
Method Summary
Modifier and TypeMethodDescriptionCreateCompareAssertfor given control XML source and actual XML source.static XmlAssertassertThat(Object o) Factory method forXmlAssertvoiddoesNotHaveXPath(String xPath) Equivalent forEquivalent forCheck if actual value is not valid against W3C XML SchemaisNotValidAgainst(Object... schemaSources) Check if actual value is not valid against schema provided by given sourcesisNotValidAgainst(Schema schema) Check if actual value is not valid against given schemaisValid()Check if actual value is valid against W3C XML SchemaisValidAgainst(Object... schemaSources) Check if actual value is valid against schema provided by given sourcesisValidAgainst(Schema schema) Check if actual value is valid against given schemanodesByXPath(String xPath) CreateMultipleNodeAssertfrom nodes selecting by given xPath.valueByXPath(String xPath) CreateValueAssertfrom value of given xPath expression.Sets theDocumentBuilderFactoryto use when creating aDocumentfrom the XML input.withNamespaceContext(Map<String, String> prefix2Uri) Utility method used for creating a namespace context mapping to be used in XPath matching.Sets theXPathFactoryto use for XPath related assertions.Methods inherited from class org.assertj.core.api.AbstractAssert
asInstanceOf, asList, assertionError, asString, describedAs, descriptionText, doesNotHave, doesNotHaveSameClassAs, equals, extracting, extracting, failure, failureWithActualExpected, failWithActualExpectedAndMessage, failWithMessage, getWritableAssertionInfo, has, hashCode, hasSameClassAs, hasSameHashCodeAs, hasToString, inBinary, inHexadecimal, is, isElementOfCustomAssert, isEqualTo, isExactlyInstanceOf, isIn, isIn, isInstanceOf, isInstanceOfAny, isInstanceOfSatisfying, isNot, isNotEqualTo, isNotExactlyInstanceOf, isNotIn, isNotIn, isNotInstanceOf, isNotInstanceOfAny, isNotNull, isNotOfAnyClassIn, isNotSameAs, isNull, isOfAnyClassIn, isSameAs, matches, matches, newListAssertInstance, overridingErrorMessage, overridingErrorMessage, satisfies, satisfies, satisfiesAnyOf, satisfiesAnyOf, satisfiesAnyOf, setCustomRepresentation, setDescriptionConsumer, setPrintAssertionsDescription, throwAssertionError, usingComparator, usingComparator, usingDefaultComparator, usingRecursiveComparison, usingRecursiveComparison, withFailMessage, withFailMessage, withRepresentation, withThreadDumpOnErrorMethods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface org.assertj.core.api.Descriptable
as, as, describedAs
-
Method Details
-
assertThat
Factory method forXmlAssert- Parameters:
o- object with type supported byInput.from(Object)- Returns:
- a fresh XmlAssert instance
-
withDocumentBuilderFactory
Sets theDocumentBuilderFactoryto use when creating aDocumentfrom the XML input.- Parameters:
dbf- factory to use- Returns:
- this
- Throws:
AssertionError- if the actual value isnull.
-
withXPathFactory
Sets theXPathFactoryto use for XPath related assertions.- Parameters:
xpf- factory to use- Returns:
- this
- Throws:
AssertionError- if the actual value isnull.
-
withNamespaceContext
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- Returns:
- this
- Throws:
AssertionError- if the actual value isnull.
-
nodesByXPath
CreateMultipleNodeAssertfrom nodes selecting by given xPath.- Parameters:
xPath- XPath expression- Returns:
- assert capturing the selected nodes
- Throws:
AssertionError- if the xPath is blank.AssertionError- if the actual value isnull.AssertionError- if the actual value provide invalid XML.
-
hasXPath
Equivalent fornodesByXPath(xPath).exist()- Parameters:
xPath- XPath expression- Returns:
- assert capturing the selected nodes
-
doesNotHaveXPath
Equivalent fornodesByXPath(xPath).doNotExist()- Parameters:
xPath- XPath expression
-
valueByXPath
CreateValueAssertfrom value of given xPath expression.- Parameters:
xPath- XPath expression- Returns:
Assertfor the node's value- Throws:
AssertionError- if the xPath is blank.AssertionError- if the actual value isnull.AssertionError- if the actual value provide invalid XML.
-
and
CreateCompareAssertfor given control XML source and actual XML source.- Parameters:
control- actual XML to compare object under test against- Returns:
- assert for comparison
- Throws:
AssertionError- if the actual value isnullAssertionError- if the control value isnull
-
isValid
Check if actual value is valid against W3C XML Schema- Returns:
- this
- Throws:
AssertionError- if the actual value isnull.AssertionError- if the actual value is invalid
-
isInvalid
Check if actual value is not valid against W3C XML Schema- Returns:
- this
- Throws:
AssertionError- if the actual value isnull.AssertionError- if the actual value is valid
-
isValidAgainst
Check if actual value is valid against given schema- Parameters:
schema- schema to validate against- Returns:
- this
- Throws:
AssertionError- if the actual value isnull.AssertionError- if the actual value is invalid
-
isNotValidAgainst
Check if actual value is not valid against given schema- Parameters:
schema- schema to validate against- Returns:
- this
- Throws:
AssertionError- if the actual value isnull.AssertionError- if the actual value is valid
-
isValidAgainst
Check if actual value is valid against schema provided by given sources- Parameters:
schemaSources- schema documents to validate against- Returns:
- this
- Throws:
AssertionError- if the actual value isnull.AssertionError- if the actual value is invalid
-
isNotValidAgainst
Check if actual value is not valid against schema provided by given sources- Parameters:
schemaSources- schema documents to validate against- Returns:
- this
- Throws:
AssertionError- if the actual value isnull.AssertionError- if the actual value is valid
-