7

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?

Luciano van der Veekens
  • 6,307
  • 4
  • 26
  • 30
Pankaj
  • 5,132
  • 3
  • 28
  • 37
  • complete code and details from this blog post: https://www.journaldev.com/21501/rest-assured-tutorial#rest-assured-xml-rest-web-services-example – Pankaj Jul 09 '18 at 16:31
  • It seems like errorResponse2/errorResponse1 are not part of the xml response.. perhaps did you try to change `Matchers.is("Wrong ID")` into `Matchers.is("Bla Bla")` and see how it reacts ? also, is there a chance that the xml you provided doesn't relate to the `response` in the test code ? perhaps you can dump the response to stdout from the code ? – Zohar81 Jul 12 '18 at 07:48
  • I reproduced OP's issue on my machine and even with `"giberishzzz.errorCode"`, the test passes. But it fails on `"errorResponse.errorCodeXXX"`.It's like the first node name is just ignored. Weird. Maybe a bug ? OP has already opened an issue 13 days ago but nobody answered (https://github.com/rest-assured/rest-assured/issues/1034) – KeatsPeeks Jul 12 '18 at 11:48
  • Thanks Keats, I forgot to mention that I had raised GitHub issue too. – Pankaj Jul 17 '18 at 04:28

1 Answers1

1

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.

Luciano van der Veekens
  • 6,307
  • 4
  • 26
  • 30