XpathResultMatcher supports Hamcrest Matcher NodeList
Use when xpath result is XPathConstants.NODESET
This commit is contained in:
committed by
Rossen Stoyanchev
parent
5ee990e045
commit
6db8306e46
@@ -103,6 +103,18 @@ public class XpathExpectationsHelper {
|
||||
MatcherAssert.assertThat("XPath " + this.expression, node, matcher);
|
||||
}
|
||||
|
||||
/**
|
||||
* Parse the content, evaluate the XPath expression as a {@link NodeList},
|
||||
* and assert it with the given {@code Matcher<NodeList>}.
|
||||
*/
|
||||
public void assertNodeList(byte[] content, @Nullable String encoding, final Matcher<? super NodeList> matcher)
|
||||
throws Exception {
|
||||
|
||||
Document document = parseXmlByteArray(content, encoding);
|
||||
NodeList nodeList = evaluateXpath(document, XPathConstants.NODESET, NodeList.class);
|
||||
MatcherAssert.assertThat("XPath " + this.getXpathExpression(), nodeList, matcher);
|
||||
}
|
||||
|
||||
/**
|
||||
* Apply the XPath expression and assert the resulting content exists.
|
||||
* @throws Exception if content parsing or expression evaluation fails
|
||||
|
||||
@@ -22,6 +22,7 @@ import javax.xml.xpath.XPathExpressionException;
|
||||
|
||||
import org.hamcrest.Matcher;
|
||||
import org.w3c.dom.Node;
|
||||
import org.w3c.dom.NodeList;
|
||||
|
||||
import org.springframework.lang.Nullable;
|
||||
import org.springframework.mock.web.MockHttpServletResponse;
|
||||
@@ -69,6 +70,17 @@ public class XpathResultMatchers {
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Evaluate the XPath and assert the {@link NodeList} content found with the
|
||||
* given Hamcrest {@link Matcher}.
|
||||
*/
|
||||
public ResultMatcher nodeList(final Matcher<? super NodeList> matcher) {
|
||||
return result -> {
|
||||
MockHttpServletResponse response = result.getResponse();
|
||||
this.xpathHelper.assertNodeList(response.getContentAsByteArray(), getDefinedEncoding(response), matcher);
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the response encoding if explicitly defined in the response, {code null} otherwise.
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user