Package org.springframework.test.util
Class XpathExpectationsHelper
java.lang.Object
org.springframework.test.util.XpathExpectationsHelper
A helper class for applying assertions via XPath expressions.
- Since:
- 3.2
- Author:
- Rossen Stoyanchev
- 
Constructor SummaryConstructors
- 
Method SummaryModifier and TypeMethodDescriptionvoidassertBoolean(byte[] content, String encoding, boolean expectedValue) Apply the XPath expression and assert the resulting content as a Boolean.voidassertNode(byte[] content, String encoding, Matcher<? super Node> matcher) Parse the content, evaluate the XPath expression as aNode, and assert it with the givenMatcher<Node>.voidassertNodeCount(byte[] content, String encoding, int expectedCount) Apply the XPath expression and assert the resulting content as an integer.voidassertNodeCount(byte[] content, String encoding, Matcher<? super Integer> matcher) Apply the XPath expression and assert the resulting content with the given Hamcrest matcher.voidassertNodeList(byte[] content, String encoding, Matcher<? super NodeList> matcher) Parse the content, evaluate the XPath expression as aNodeList, and assert it with the givenMatcher<NodeList>.voidassertNumber(byte[] content, String encoding, Double expectedValue) Apply the XPath expression and assert the resulting content as a Double.voidassertNumber(byte[] content, String encoding, Matcher<? super Double> matcher) Apply the XPath expression and assert the resulting content with the given Hamcrest matcher.voidassertString(byte[] content, String encoding, String expectedValue) Apply the XPath expression and assert the resulting content as a String.voidassertString(byte[] content, String encoding, Matcher<? super String> matcher) Apply the XPath expression and assert the resulting content with the given Hamcrest matcher.voiddoesNotExist(byte[] content, String encoding) Apply the XPath expression and assert the resulting content does not exist.<T> TevaluateXpath(byte[] content, String encoding, Class<T> targetClass) Evaluate the XPath and return the resulting value.protected <T> TevaluateXpath(Document document, QName evaluationType, Class<T> expectedClass) Apply the XPath expression to given document.voidApply the XPath expression and assert the resulting content exists.protected XPathExpressionReturn the compiled XPath expression.protected DocumentparseXmlByteArray(byte[] xml, String encoding) Parse the given XML content to aDocument.
- 
Constructor Details- 
XpathExpectationsHelperpublic XpathExpectationsHelper(String expression, @Nullable Map<String, String> namespaces, Object... args) throws XPathExpressionExceptionXpathExpectationsHelper constructor.- Parameters:
- expression- the XPath expression
- namespaces- the XML namespaces referenced in the XPath expression, or- null
- args- arguments to parameterize the XPath expression with using the formatting specifiers defined in- String.format(String, Object...)
- Throws:
- XPathExpressionException- if expression compilation failed
 
 
- 
- 
Method Details- 
getXpathExpressionReturn the compiled XPath expression.
- 
assertNodepublic void assertNode(byte[] content, @Nullable String encoding, Matcher<? super Node> matcher) throws Exception Parse the content, evaluate the XPath expression as aNode, and assert it with the givenMatcher<Node>.- Throws:
- Exception
 
- 
assertNodeListpublic void assertNodeList(byte[] content, @Nullable String encoding, Matcher<? super NodeList> matcher) throws Exception Parse the content, evaluate the XPath expression as aNodeList, and assert it with the givenMatcher<NodeList>.- Throws:
- Exception
- Since:
- 5.2.2
 
- 
existsApply the XPath expression and assert the resulting content exists.- Throws:
- Exception- if content parsing or expression evaluation fails
 
- 
doesNotExistApply the XPath expression and assert the resulting content does not exist.- Throws:
- Exception- if content parsing or expression evaluation fails
 
- 
assertNodeCountpublic void assertNodeCount(byte[] content, @Nullable String encoding, Matcher<? super Integer> matcher) throws Exception Apply the XPath expression and assert the resulting content with the given Hamcrest matcher.- Throws:
- Exception- if content parsing or expression evaluation fails
 
- 
assertNodeCountpublic void assertNodeCount(byte[] content, @Nullable String encoding, int expectedCount) throws Exception Apply the XPath expression and assert the resulting content as an integer.- Throws:
- Exception- if content parsing or expression evaluation fails
 
- 
assertStringpublic void assertString(byte[] content, @Nullable String encoding, Matcher<? super String> matcher) throws Exception Apply the XPath expression and assert the resulting content with the given Hamcrest matcher.- Throws:
- Exception- if content parsing or expression evaluation fails
 
- 
assertStringpublic void assertString(byte[] content, @Nullable String encoding, String expectedValue) throws Exception Apply the XPath expression and assert the resulting content as a String.- Throws:
- Exception- if content parsing or expression evaluation fails
 
- 
assertNumberpublic void assertNumber(byte[] content, @Nullable String encoding, Matcher<? super Double> matcher) throws Exception Apply the XPath expression and assert the resulting content with the given Hamcrest matcher.- Throws:
- Exception- if content parsing or expression evaluation fails
 
- 
assertNumberpublic void assertNumber(byte[] content, @Nullable String encoding, Double expectedValue) throws Exception Apply the XPath expression and assert the resulting content as a Double.- Throws:
- Exception- if content parsing or expression evaluation fails
 
- 
assertBooleanpublic void assertBoolean(byte[] content, @Nullable String encoding, boolean expectedValue) throws Exception Apply the XPath expression and assert the resulting content as a Boolean.- Throws:
- Exception- if content parsing or expression evaluation fails
 
- 
evaluateXpath@Nullable public <T> T evaluateXpath(byte[] content, @Nullable String encoding, Class<T> targetClass) throws Exception Evaluate the XPath and return the resulting value.- Parameters:
- content- the content to evaluate against
- encoding- the encoding to use (optionally)
- targetClass- the target class, one of Number, String, Boolean, org.w3c.Node, or NodeList
- Throws:
- Exception- if content parsing or expression evaluation fails
- Since:
- 5.1
 
- 
parseXmlByteArrayParse the given XML content to aDocument.- Parameters:
- xml- the content to parse
- encoding- optional content encoding, if provided as metadata (e.g. in HTTP headers)
- Returns:
- the parsed document
- Throws:
- Exception
 
- 
evaluateXpath@Nullable protected <T> T evaluateXpath(Document document, QName evaluationType, Class<T> expectedClass) throws XPathExpressionException Apply the XPath expression to given document.- Throws:
- XPathExpressionException- if expression evaluation failed
 
 
-