Package org.xmlunit.matchers
Class HasXPathMatcher
java.lang.Object
org.hamcrest.BaseMatcher<Object>
org.xmlunit.matchers.HasXPathMatcher
- All Implemented Interfaces:
org.hamcrest.Matcher<Object>
,org.hamcrest.SelfDescribing
This Hamcrest
Matcher
verifies whether the provided XPath expression corresponds to at least
one element in the provided object.
All types which are supported by Input.from(Object)
can be used as input for the object
against the matcher is evaluated.
Simple Example
final String xml = "<a><b attr=\"abc\"></b></a>"; assertThat(xml, hasXPath("//a/b/@attr")); assertThat(xml, not(hasXPath("//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(xmlRootElement, hasXPath("//atom:feed/atom:entry/atom:id").withNamespaceContext(prefix2Uri));
- Since:
- XMLUnit 2.1.0
-
Constructor Summary
ConstructorDescriptionHasXPathMatcher
(String xPath) Creates aHasXPathMatcher
instance with the associated XPath expression. -
Method Summary
Modifier and TypeMethodDescriptionvoid
describeMismatch
(Object item, org.hamcrest.Description mismatchDescription) void
describeTo
(org.hamcrest.Description description) static HasXPathMatcher
Creates a matcher that matches when the examined XML input has at least one node corresponding to the specifiedxPath
.boolean
Sets theDocumentBuilderFactory
to use when creating aDocument
from the XML input.withNamespaceContext
(Map<String, String> prefix2Uri) Utility method used for creating a namespace context mapping to be used in XPath matching.Sets theXPathFactory
to use.Methods inherited from class org.hamcrest.BaseMatcher
_dont_implement_Matcher___instead_extend_BaseMatcher_, toString
-
Constructor Details
-
HasXPathMatcher
Creates aHasXPathMatcher
instance with the associated XPath expression.- Parameters:
xPath
- xPath expression
-
-
Method Details
-
withDocumentBuilderFactory
Sets theDocumentBuilderFactory
to use when creating aDocument
from the XML input.- Parameters:
f
- the DocumentBuilderFactory to use- Returns:
- this
- Since:
- XMLUnit 2.6.0
-
withXPathFactory
Sets theXPathFactory
to use.- Parameters:
f
- the XPathFactory to use- Returns:
- this
- Since:
- XMLUnit 2.6.1
-
matches
-
describeTo
public void describeTo(org.hamcrest.Description description) -
describeMismatch
-
hasXPath
Creates a matcher that matches when the examined XML input has at least one node corresponding to the specifiedxPath
.For example:
assertThat(xml, hasXPath("/root/cars[0]/audi"))
- Parameters:
xPath
- the target xpath- Returns:
- the xpath matcher
-
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
-