My example web service is returning following XML.
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<errorResponse>
<errorCode>Wrong ID</errorCode>
<errorId>2</errorId>
</errorResponse>
Following tests are passed.
response.then().body("errorResponse.errorId", Matchers.is("2"));
response.then().body("errorResponse.errorCode", Matchers.is("Wrong ID"));
response.then().body("errorResponse1.errorCode", Matchers.is("Wrong ID"));
response.then().body("errorResponse2.errorCode", Matchers.is("Wrong ID"));
I understand that first two tests are fine, what I am not getting is why the last two are getting passed?
Rest-assured uses its xml-path library to evaluate your hamcrest matcher and that library contains the XMLAssertion class which does the actual checking.
The source can be found on GitHub: https://github.com/rest-assured/rest-assured/blob/master/xml-path/src/main/groovy/io/restassured/assertion/XMLAssertion.groovy
At line 60 of this file you can see it removes the part of the search key before the first dot, because it recognizes we're evaluating from a root node.
Hence your key:
"errorResponse3.errorCode"
becomes
".errorCode"
So it turns out it doesn't matter what this initial path looks like, it assumes it's the name of the root node and discards it anyway.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With