简体   繁体   中英

Check content of entire JSON Array in SOAP UI groovy script

I have the following JSON response:

{
   "matrix":[
      {
         "abc":{
            "type":"dec"
         }
      },
      {
         "abc":{
            "type":"dec"
         }
      }
   ]
}

how can I put assertion on entire content of "matrix" array, without converting the JSON response to XML?

Parsed JSON in groovy is a common map. You can do some validations agains this map. Eg

def list = [[id:'1',no:'1'],[id:'2',no:'3']]
assert list.every { it.id.isNumber() }
assert list.every { it.keySet().contains('val') }

Or JSON schema..

import groovy.json.JsonSlurper

def m = """{
    "matrix" : [
        {
           "abc" : {
               "type":"dec"
           },
        },
        {
           "abc" : {
               "type":"dec"
           }
        }
     ]
}""" 
def parsed = new JsonSlurper().parseText(m) 
assert parsed.matrix.every { it.abc.type in ['dec'] }

def ResponseMessage =  testRunner.testCase.testSteps["TestStepName"].testRequest.response.contentAsStri‌​ng 
def jsonSlurper = new JsonSlurper().parseText(ResponseMessage) 
assert  jsonSlurper.matrix.every {it.abc.type in ['dec']}

找出路

assert jsonSlurper.matrix.toString() == '["abc":{"type":"dec"}]'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM