Groovy Assertion

时间:2018-03-10 08:33:32

标签: groovy soapui assert

在SOAP UI中,当我尝试使用Google Maps API的测试用例(放置地址 - 文本搜索)的自定义属性值从响应中声明纬度值时,尽管值相同,但使用Groovy的断言失败。可能的原因是什么?。

API调用

https://maps.googleapis.com/maps/api/place/textsearch/json?query=Chennai+One&key=<developerKey>

返回:

{
  "html_attributions": [],
  "results": [
    {
      "formatted_address": "Pallavaram Ring Road, Pallavaram-Thuraipakkam Rd, MCN Nagar Extension, Thoraipakkam, Chennai, Tamil Nadu 600097, India",
      "geometry": {
        "location": {
          "lat": 12.947878,
          "lng": 80.2319971
        },
        "viewport": {
          "northeast": {
            "lat": 12.94931362989272,
            "lng": 80.23338167989272
          },
          "southwest": {
            "lat": 12.94661397010728,
            "lng": 80.23068202010727
          }
        }
      },
      "icon": "https://maps.gstatic.com/mapfiles/place_api/icons/geocode-71.png",
      "id": "8b73bf226e8b0b6bb68e9679cbbc3a81d473d809",
      "name": "Channai 1 Etc Tower",
      "photos": [
        {
          "height": 3120,
          "html_attributions": [
            "<a href=\"https://maps.google.com/maps/contrib/105169389690785685237/photos\">Vijin VL</a>"
          ],
          "photo_reference": "CmRZAAAA2fCq69MGZM6NHWf_7DNCBreYqSH1xKZNHKPvOf90GJVJYFdsXT3p1BkLSNFNk7fBVSG-CsmEtjxnQ5C40UvxQVgPjqOhapnqKtQStkDF_pdU_r63tvuEFMZHT_lLc5dOEhDn-a50Y16QvNV25A28_q0QGhTi7KH-rIFzatMXKWBOnU6Euy9HYA",
          "width": 4160
        }
      ],
      "place_id": "ChIJ9ZZvD6pdUjoRb0L40z75yjs",
      "reference": "CmRbAAAAU3HluWseu0ebnBgQ4X5WOUAujSTZM4k4w0pLM7h1HsYpZtrFj1QvnlRKCA2qin09nnU79yiR72m75NPzL2ctzNeH8jr48rwebLyvww62miCihbXMHs7c1ahveSmBdbhmEhBdeLkvELQ59C6kA3ImAzkAGhSNVmd2Ex9y2ouN8dcK0YIClAMrww",
      "types": [
        "premise"
      ]
    }
  ],
  "status": "OK"
}

Groovy Assertion:

import groovy.json.JsonSlurper
def res= messageExchange.response.responseContent
def json1=new JsonSlurper().parseText(res)
def act=json1.results[0].geometry.location.lat
log.info act
def exp=context.getTestCase().getPropertyValue("lat")
log.info exp
assert act==exp

1 个答案:

答案 0 :(得分:1)

actBigDecimalexpString。如果将它们转换为相同类型,则可以成功比较它们:

import groovy.json.JsonSlurper
def res= messageExchange.response.responseContent
def json1=new JsonSlurper().parseText(res)

def act=json1.results[0].geometry.location.lat
log.info(act.getClass()) // BigDecimal
log.info act
def String actString = act

def exp=context.getTestCase().getPropertyValue("lat")
log.info(exp.getClass()) // String
log.info exp

assert actString.equals(exp)

soapUI还有一个方便的内置JSON路径匹配器,因此如果您不想,您不必编写自己的断言代码:

jsonpath matcher