1

I have json for example below

{"TestJson":{  
  "Result":"Passed",
  "description":"Passed."},
  "Students":[{  
     "Class":{  
        "Primary":"Yes"
     },
     "Course":{  
        "Enrolled":"yes",
         "AccountNumber":"2387287382"
     },
     "AccountNumber":"2387287382",
     "Paid":"Yes"
  }]}

I am wondering how can I find a good solution for this.

What I currently do

.body("Students[0].Course.AccountNumber",equalTo("2387287382"))
.body("Students[0].AccountNumber",equalTo("2387287382"))

My test criteria is to check key Students[0].AccountNumber matches Students[0].Course.AccountNumber


I want to do in this way, but i am not able to find a solution something like

.body("Students[0].Course.AccountNumber",equalTo("Students[0].AccountNumber"))

The above wont work obviously, but that is how I want to compare. basically comparing the key with another key and they should match.

Is this doable?

roy
  • 81
  • 1
  • 7

2 Answers2

1

One way to do it is:

String A = 
given ().
when().
get/put/post({api/name})
.extract()
.path("Students[0].Course.AccountNumber");

String B = 
given ().
when().
get/put/post({api/name})
.extract()
.path("Students[0].AccountNumber");

Assert.assertEquals(A, B);

Seems like this workaround is the only way to go.

roy
  • 81
  • 1
  • 7
0

See the Use the response to verify other parts of the response section of the rest-assured docs. You basically want to create a lambda implementing ResponseAwareMatcher<Response>. Something like this:

get("/x").then().body("href", response -> equalTo("http://localhost:8080/" + response.path("userId"));