【问题标题】:RestAssured: how to iterate through JSON object and validate each value of key fieldsRestAssured:如何遍历 JSON 对象并验证关键字段的每个值
【发布时间】:2020-02-10 21:38:38
【问题描述】:
{
  "body": {
    "format": "json",
    "query": {
      "attributes": [
        {
          "field": "name",
          "order": "none",
          "hidden": false,
          "label": "Computer"
        }
      ],
      "types": [],
      "detail": true,
      "filter": {},
      "size": 5,
      "types_group": "test",
      "context": "newtest"
    },
    "bookmark": {
      "guid": null,
      "processed_time": 1570570800000
    }
  },
  "description": "test new",
  "display_name": "test1",
  "type": "profile",
  "read": "all",
  "write": "all"
}

如何遍历这个 JSON 对象并验证 RestAssured 框架中键的每个值。

我想为这个 JSON 中的每个键比较类似的东西:

AssertTrue(get("display_name").matches("test1"))

【问题讨论】:

    标签: java json rest-assured


    【解决方案1】:

    因为这对其他人非常有用,所以尽管回答它。 但是在回答之前,有一个问题-您将如何提供预期的数据集以与实际响应进行比较?因为您要验证整个响应主体,所以它非常重要。 我将解释来自 Cucumber Data 表的控制数据;就像现在一样,请放心,主要与 Cucumber(BDD) 一起使用,所以我。(也可以使用 TestNG 数据提供程序作为测试数据 m/m 的一种有效方式,如果需要,我也可以解释一下..plz 让我知道) 无论如何回答下面是方法1:

    示例功能文件:

     When User calls API- "yourAPI" with "Get/Post" method
        Then Server should return response code as "200"
        And Verify Response body contents matched with expected values
          | key     | value                            |
          | status  | 200                              |
          | message | Success                          |
          | result  | Batch got generated successfully |

    上述响应验证步骤的步骤定义:

    @Then("^Verify Response body contents matched with expected values$")
        public void validateEntireJsonRespBody(DataTable table) throws Throwable {
            List<List<String>> data = table.raw();
            var resBody = response.getBody(); // capture the response in var as per your FW or methods being used
            for(int i = 1; i < data.size(); i++) {
                System.out.println(" Actual Response Value: "+resBody.jsonPath().get(data.get(i).get(0)).toString()+" To be Matched with Expected Data Table value: "+data.get(i).get(1));
                                assertEquals(data.get(i).get(1),resBody.jsonPath().get(data.get(i).get(0)).toString());
          // Or             
            assertThat(resBody.jsonPath().get(data.get(i).get(0)).toString(), equalToCompressingWhiteSpace(data.get(i).get(1)));
    
            }
    

    或者同样可以通过这种方式完成 - ex2:如果您使用的是 cucumber.io 而不是 info.cukes。

    And response includes the following
          | key                              | value              |
          | id                               | 100000002          |
          | batchCode                        | USFP               |
          | batchName                        | US Faster Paym     |

    这一步的步骤定义-

        @And("response includes the following$")
        public void response_equals(Map<String,String> responseFields){
            json=response.then();
            System.out.println("JsonBody: "+json.extract().body().asString());
    
            for (Map.Entry<String, String> field : responseFields.entrySet()) {
                if(StringUtils.isNumeric(field.getValue())){
                    json.body(field.getKey(), equalTo(Integer.parseInt(field.getValue())));
                }
                else{
                    json.body(field.getKey(), equalTo(field.getValue()));
                }
            }
        }
    
    here var json was declared as of type- ValidatableResponse

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-03-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-07-21
      • 1970-01-01
      • 2021-12-10
      • 1970-01-01
      相关资源
      最近更新 更多