json响应的脚本断言

时间:2016-04-19 10:55:37

标签: json groovy soapui

我尝试在SoapUI中使用脚本断言进行json响应,但是出现了错误

  

“Lexing在线失败:1,列:1,在阅读'<'时,无法识别可能的有效JSON值或标点符号。”

请求

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:loc="http://localhost">
   <soapenv:Header/>
   <soapenv:Body>
      <loc:GetRoomAttribute>
         <loc:countryId>123</loc:countryId>
         <loc:locationId>36</loc:locationId>
         <loc:roomId>213</loc:roomId>
      </loc:GetRoomAttribute>
   </soapenv:Body>
</soapenv:Envelope>

响应

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
   <soap:Body>
      <GetRoomAttributeResponse xmlns="http://localhost">
         <GetRoomAttributeResult>{"roomAttribute":{"EndPointType":"SR","GroupId":23,"RegionId":22,"Occupancy":6}}</GetRoomAttributeResult>
      </GetRoomAttributeResponse>
   </soap:Body>
</soap:Envelope>

脚本断言

import groovy.json.JsonSlurper

def response = messageExchange.response.responseContent
def slurper = new JsonSlurper()
def json = slurper.parseText response

assert json.roomAttribute.EndPointType == "SR"

如何使用'EndPointType','GroupId','RegionId','Occupancy'类使用脚本断言来JsonSlurper断言。

1 个答案:

答案 0 :(得分:0)

以下是json请求属性的脚本断言。为脚本的每一行添加了注释内容。

import groovy.json.*
import com.eviware.soapui.support.XmlHolder
def xml = '''
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <soap:Body>
    <GetRoomAttributeResponse xmlns="http://localhost">
      <GetRoomAttributeResult>{"roomAttribute":{"EndPointType":"SR","GroupId":23,"RegionId":22,"Occupancy":6}}</GetRoomAttributeResult>
    </GetRoomAttributeResponse>
  </soap:Body>
</soap:Envelope>'''
//response xml holder
def response = new XmlHolder(xml)
/*you may replace the above constant xml using context.response in the above line i.e new XmlHolder(context.response) and also remove def xml.. line too */
//read the json data from xml using xpath
def data = response.getNodeValue("//*:GetRoomAttributeResult")
//log the json data
log.info data
//read roomAtribute from json using json slurper
def roomAttribute  = new groovy.json.JsonSlurper().parseText( data.toString()).roomAttribute 
//Assert EndPointType property is not  present  in the response
assert  null != roomAttribute.EndPointType, "Endpoint type property does not exists or null"
//Assert EndPointType Value
assert  roomAttribute.EndPointType , "Endpoint type does not have value"
//Assert GroupId property is not  present  in the response
assert  null !=roomAttribute.GroupId, "GroupId property does not exists or null"
//Assert GroupId Value
assert  roomAttribute.GroupId, "GroupId does not have value"
//Assert RegionId property is not  present  in the response
assert  null != roomAttribute.RegionId, "RegionId property does not exists or null"
//Assert RegionId Value
assert  roomAttribute.RegionId, "RegionId does not have value"
//Assert Occupancy property is not  present  in the response
assert  null != roomAttribute.Occupancy, "Occupancy property does not exists or null"
//Assert Occupancy Value
assert  roomAttribute.Occupancy, "Occupancy does not have value"
//log the values
log.info "Endpoint Type  : ${roomAttribute.EndPointType}"
log.info "Group Id  : ${roomAttribute.GroupId}"
log.info "Region Id  : ${roomAttribute.RegionId}"
log.info "Occupancy : ${roomAttribute.Occupancy}"