【问题标题】:rest assured check the name is exist in the json response放心检查名称是否存在于 json 响应中
【发布时间】:2015-05-11 06:12:35
【问题描述】:

我是新来的放心。使用“放心”尝试验证是否找到数据详细信息。这里存在两个数据详细信息。有时会是 2 或 3 或 5 我得到如下响应并使用 java

{
  "queryPath": "/api/",
  "nId": "f084f5ad24fcfaa9e9faea0",
  "statusCode": 707
  "statusMessage": "Success",
  "results": {
    "data": [
      {
        "id": "10248522500798",
        "capabilities": [
          "record",
          "HDt"
        ],
        "name": "errt2"
      },
      {
        "id": "418143778",
        "capabilities": [
          "1record",
          "HDy"
        ],
        "name": "Livin"
      }
    ]
  }
}

代码使用

JsonPath jsonResponse = new JsonPath(response.asString());
ArrayList<String> list = new ArrayList<String>();
list = jsonResponse.get("results.data"); // 

if (list.size() < 1 ) {
    SpiceCheck.fail("data not found! " + list.size());
}

我还想检查数据名称是否为空。我怎样才能放心。

【问题讨论】:

    标签: java json rest hamcrest rest-assured


    【解决方案1】:

    只是为了让您知道您在 707 之后缺少一个逗号。

    为了验证没有一个名称为空,我会将名称解析为一个列表,然后逐个迭代名称并检查它们是否为空。

    List<String> names = from(response.asString()).getList("results.data.name");
    for(String name : names){
      if(name == null){
         // error handling code here
      }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-03-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-03-31
      相关资源
      最近更新 更多