【问题标题】:Rest-Assured - json body assertion for set of values in list regardless of positionRest-Assured - 列表中一组值的 json 主体断言,无论位置如何
【发布时间】:2017-10-06 14:25:23
【问题描述】:

我正在使用 rest-assured 来测试一个 rest api。我有一个返回对象数组的 json 响应。我想验证它是否包含具有特定值测试的对象。检查以下示例:

示例 JSON:

{
    "contents" : [
         {  
              "field1" : "value1",
              "field2" : "value2"
         },
         {  
              "field1" : "value3",
              "field2" : "value4"
         }
    ]
}

如何编写正文断言,以便无论我在哪个位置都有一个条目,我都可以检查:

value1 & value2 在每个相应的字段中 以及另一个带有 value3 和 value4 的条目。

...get ("/myEndpoint" )
.then()
.body ( "contents.?????", contains...)

【问题讨论】:

    标签: json rest spring-boot rest-assured rest-assured-jsonpath


    【解决方案1】:

    取决于您所说的“无论职位如何”。

    如果您的意思是无论在contents 中的位置如何,但重要的是field1field2 之前指定,您可以这样做:

    when().
            get("/myEndpoint").
    then().
            root("contents.any { it == ['field1':'%s', 'field2':'%s'] }").
            body(withArgs("value1", "value2"), is(true)).
            body(withArgs("value3", "value4"), is(true));
    

    如果您不关心 field1 是否在 field2 之前指定,您可以这样做:

    when().
            get("/myEndpoint").
    then().
            root("contents").
            body("", hasItems(hasEntry("field2", "value2"), hasEntry("field1", "value1")),
                 "", hasItems(hasEntry("field1", "value3"), hasEntry("field2", "value4")));
    

    您可以阅读“根路径”here

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-10-07
      • 2022-01-08
      • 2019-10-24
      • 1970-01-01
      • 1970-01-01
      • 2017-08-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多