[英]Validate xml response/string against list of xsds using RestAssured
[英]How to validate a list of values from the response using RestAssured
有人可以請我了解如何驗證響應中的項目列表。 假設響應如下所示,
{
"store":{
"book":[
{
"author":"Nigel Rees",
"category":"reference",
"price":8.95,
"title":"Sayings of the Century"
},
{
"author":"Evelyn Waugh",
"category":"fiction",
"price":12.99,
"title":"Sword of Honour"
},
{
"author":"Herman Melville",
"category":"fiction",
"isbn":"0-553-21311-3",
"price":8.99,
"title":"Moby Dick"
},
{
"author":"J. R. R. Tolkien",
"category":"fiction",
"isbn":"0-395-19395-8",
"price":22.99,
"title":"The Lord of the Rings"
}
]
}
}
元素 Book 下有四個包含不同數據的列表,現在如果我想按順序驗證作者姓名和價格(例如在循環中),我該如何實現..?
我通常將響應轉換為 Json 文檔然后進行驗證,但在這種情況下,如果我使用響應中的四個列表中的 Json 路徑“Store.book.author”,它會引用哪個列表? 這就是我的困惑所在。
有一個放心的內置方法,您可以使用它來獲取 Array 的所有項目作為地圖列表。
String key="book";//array key (as it mentioned in your Json)
Response response=//your API call which will return Json Object
List<Hash<String,Object>>booksList=response.jsonPath().getList(key);
//Now parse value from List
Hash<String,Object> firstBookDetails=booksList.get(0);// for first index
String author=(String)firstBookDetails.get("author");
將 Rest Assured 與 BDD 結合使用時,您可以嘗試一下。
given()
.when()
.get(API URL)
.then()
.assertThat().body("store.book.author[0]", equalTo("Nigel Rees"));
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.