Forum Discussion

AAB's avatar
AAB
Regular Contributor
5 years ago
Solved

ReadyAPI script assertion 'is not equal' to for JSON response

Greetings,   I would like to assert that certain values are not present in the response of my REST request. I'm using ReadyAPI V2.8.0. when sending the endpoint I'm expecting only values for ...
  • AAB's avatar
    AAB
    4 years ago

    Hello,

     

    I've looked into this with a developper. For our response we see that we actually have an array in an array. Therefore the code needed to be adapted a bit.

    The response looks like this:

    Tue Sep 22 16:30:58 CEST 2020: INFO: [[nl], [nl], [nl], [nl], [nl], [nl], [nl], [nl], [nl], [nl], [nl], [nl], [nl], [nl], [nl], [nl], [nl], [nl], [nl], [nl], [nl], [nl], [nl], [nl], [nl], [nl], [nl], [nl], [nl], ......etc..]

    Assertion second part:

     

    .....
    
    def checkForLanguage = "nl"
    
    def onlyExpectedLanguage = true
    language.each {
      element -> element.each {
      		languageUnderTest -> {
      				if (!languageUnderTest.equals(checkForLanguage)) {
      						onlyExpectedLanguage = false
      						log.info "Found unexpected language: ${languageUnderTest}"
      				}
      		}
      }
    }
    
    assert onlyExpectedLanguage, "Found a different language than ${checkForLanguage}"

     

    In this case I was, with the previous code, asking to compare a string (def checkForLanguage = "nl")  with an array ( def language = json.pdcData.concepts.description.language ) and thus of course the code passed each time because it was comparing something (array) with nothing (string).

    So in order to be able to compare, I needed to loop over each array in that array.

    Mostly the code is written like this:

     

    def expected = true
    x.each{
         y -> y.each{
               variableName -> {
                                 //your statement here
                               }
              }
         }
    assert expected, "error message here"

     

     

    Thanks for your help anyways 😄 

    Kind regards,

    AboveAndBeyond