首页 > 解决方案 > 空手道 DSL 断言嵌套 json

问题描述

{"serviceName":"Legal Entity account for given input account.","requestTime":1545426348945,"responseTime":1545426348949,"timeTaken":4,"responseCode":0,"responseMessage":"Success","pageSize":100,"pageNumber":0,"accounts":{"transferDate":1549429200000,"migrationWave":"5","searchedLEAccount":{"accountNumber":"41477514","cbdNumber":"12345678","bic":"CHASGBXxX","poolAccount":"Y","sweepMasterAccount":"Y","status":"DORMANT","branchId":"000000071","branchName":"LONDON","leAccountType":"OLD"},"linkedLEAccount":{"accountNumber":"6541245045","cbdNumber":"854321","bic":"CHASLUY","status":"DORMANT","branchId":"000000055","branchName":"S.A","leAccountType":"NEW"}}}

我正在尝试获取所有 accountNumber 并验证它们是否是数字。我究竟做错了什么?

When method Post

Then status 200

And match response != null

And match response contains {serviceName: 'Legal Entity account for given input account.' }

And match response.accounts.searchedLEAccount contains { accountNumber: '#notnull' }

And match response.accounts.searchedLEAccount contains { accountNumber: '#present' }

And match response.accounts.searchedLEAccount contains { accountNumber: '#number' }

标签: javaautomationautomated-testskarate

解决方案


在一行中:

* match each $..accountNumber == '#regex \\d+'

提示:仔细阅读文档并理解 Json-Path。

这是完整的示例,您可以将其粘贴到新示例中Scenario并查看工作:

* def response = 
"""
{
   "serviceName":"Legal Entity account for given input account.",
   "requestTime":1545426348945,
   "responseTime":1545426348949,
   "timeTaken":4,
   "responseCode":0,
   "responseMessage":"Success",
   "pageSize":100,
   "pageNumber":0,
   "accounts":{
      "transferDate":1549429200000,
      "migrationWave":"5",
      "searchedLEAccount":{
         "accountNumber":"41477514",
         "cbdNumber":"12345678",
         "bic":"CHASGBXxX",
         "poolAccount":"Y",
         "sweepMasterAccount":"Y",
         "status":"DORMANT",
         "branchId":"000000071",
         "branchName":"LONDON",
         "leAccountType":"OLD"
      },
      "linkedLEAccount":{
         "accountNumber":"6541245045",
         "cbdNumber":"854321",
         "bic":"CHASLUY",
         "status":"DORMANT",
         "branchId":"000000055",
         "branchName":"S.A",
         "leAccountType":"NEW"
      }
   }
}
"""
* match each $..accountNumber == '#regex \\d+'

推荐阅读