空手道DSL断言嵌套json

时间:2018-12-22 02:50:03

标签: java automation automated-tests karate

{"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并验证它们是否为数字。我在做什么错

Post方法时

然后状态为200

然后匹配响应!= null

并且匹配响应包含{serviceName:“给定输入帐户的法律实体帐户”。 }

并匹配response.accounts.searchedLEAccount包含{accountNumber:'#notnull'}

并匹配response.accounts.searchedLEAccount包含{accountNumber:'#present'}

并匹配response.accounts.searchedLEAccount包含{accountNumber:'#number'}

1 个答案:

答案 0 :(得分:1)

一行:

* 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+'