【问题标题】:Cucumber:Assert values irrespective of IndexCucumber:断言值与索引无关
【发布时间】:2021-12-31 16:58:43
【问题描述】:
我正在使用 Cucumber 功能文件更新功能测试套件。我的输出问题是一个未排序的数组。对象的索引可能会更改。
数组:
[{
"id": "12",
"name": "Something"
},
{
"id": "13",
"name": "Another Something"
}
]
这里我只想在 Id=13 时声明名称。任何帮助将不胜感激。
【问题讨论】:
标签:
java
testing
cucumber
cucumber-java
cucumber-junit
【解决方案1】:
Json 是Array 中的对象列表,因此您需要解析它们并验证每个Object。你可以像下面这样,
代码:
JSONArray jsonArray = new JSONArray(JsonAsString);
JSONObject jsonObject;
for (int i = 0; i < jsonArray.length(); i++) {
jsonObject = new JSONObject(jsonArray.get(i).toString());
if (jsonObject.get("id").toString().equalsIgnoreCase("13")) {
System.out.println("Name: " + jsonObject.get("name"));
//do your thing...
}
}
输出:
Name: Another Something