提交 6db8306e 编写于 作者: P Pat Turner 提交者: Rossen Stoyanchev

XpathResultMatcher supports Hamcrest Matcher NodeList

Use when xpath result is XPathConstants.NODESET
上级 5ee990e0
......@@ -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.
*/
......
......@@ -48,6 +48,16 @@ public class XpathResultMatchersTests {
new XpathResultMatchers("/foo/bar", null).node(Matchers.nullValue()).match(getStubMvcResult()));
}
@Test
public void nodeList() throws Exception {
new XpathResultMatchers("/foo/bar", null).nodeList(Matchers.notNullValue()).match(getStubMvcResult());
}
@Test(expected = AssertionError.class)
public void nodeListNoMatch() throws Exception {
new XpathResultMatchers("/foo/bar", null).nodeList(Matchers.nullValue()).match(getStubMvcResult());
}
@Test
public void exists() throws Exception {
new XpathResultMatchers("/foo/bar", null).exists().match(getStubMvcResult());
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册