1

I'm currently writing some tests in ReadyAPI to test a new API that we are building. The API will return JSON in the following format:

{
   "testJSON" : [
      {
         "test1" : "111",
         "test2" : "ABC"
      },
      {
         "test1" : "222",
         "test2" : "DEF"
      }
   ]
}

I have added the following Groovy code that breaks down the JSON and lists each value:

import groovy.json.JsonSlurper 


def jsonResp = context.expand( '${GetTest#Response}' )
def jsonElements = new JsonSlurper().parseText(jsonResp)
for ( value in jsonElements.testInput ) {
    value.each{
            log.info "${it.key}:${it.value}"
    }
}

What I am trying to do is to take this JSON response and compare it to a pre-defined list of data (using a Data Source within ReadyAPI). This data source will contain a column titled 'test1' and a column titled 'test2' -these will contain all the values expected for each object. So we would need to ensure that each test1 value from the datasource is displayed in the response JSON. And we'd also like to confirm that for each 'test1' we get the correct corresponding 'test2'.

So in example above, we'd want to prove that the response contained 'test1' values of 111 and 222, and that for 111 we got a 'test2' value of ABC etc.

The example above is a simplistic view of our new API and in reality it will contain a much larger number of fields and a lot more responses. So ideally, I don't want to have to hard code specific values within the Groovy script - which is why I'm trying to use a data sheet.

Has anyone had any experience of this sort of thing and can make any suggestions?

Thanks in advance for your help, please let me know if you need any more information.

Cheers, Matt

Matt
  • 11
  • 1

1 Answers1

1

"testJSON" is a list of objects containing properties "list1" and "list2". Your datasource is also a list of objects (rows) containing properties (columns) "list1" and "list2".

Use a nested for loop to compare each expected object with each object in the response. Use a boolean flag to track whether test1 is missing from the response. When test1 is found, check the value of test2.

Here's the basic outline. I'm assuming test1 is a unique identifier for each object. You may also want to exit the inner loop when you do find test1, to be more efficient.

for (element in datasource) {
    def foundTest1 = false
    for (response in testJSON) {
        if (response.test1 == element.test1){
            foundTest1 = true
            assert response.test2 == element.test2
            // assert any number of other properties here
        }
    }
    assert foundTest1 == true
}
Marie
  • 149
  • 9
  • Thanks Marie. I've added the above but my assertions are constantly failing. Based on the code in my original post, I've added the following line to allow me to see what the problem is: log.info jsonElements.test1 This value always returns null. So my assertion is failing because my expected result from the datasheet is not null. Any idea why this is happening? – Matt Sep 25 '18 at 09:51
  • It could be that your jsonElements does not have a key called test1. Try logging jsonElements itself and see if the structure looks like you expect. Another possibility is if your values are not integers, you may need to use something other than == to compare them. – Marie Sep 25 '18 at 22:29