【问题标题】:Rest Assured - comparing the json path with another放心 - 将 json 路径与另一个路径进行比较
【发布时间】:2019-01-24 20:45:26
【问题描述】:

我有json例如下面

{"TestJson":{  
  "Result":"Passed",
  "description":"Passed."},
  "Students":[{  
     "Class":{  
        "Primary":"Yes"
     },
     "Course":{  
        "Enrolled":"yes",
         "AccountNumber":"2387287382"
     },
     "AccountNumber":"2387287382",
     "Paid":"Yes"
  }]}

我想知道如何找到一个好的解决方案。

我目前在做什么

.body("Students[0].Course.AccountNumber",equalTo("2387287382"))
.body("Students[0].AccountNumber",equalTo("2387287382"))

我的测试标准是检查键 Students[0].AccountNumber 匹配 Students[0].Course.AccountNumber


我想这样做,但我找不到类似的解决方案

.body("Students[0].Course.AccountNumber",equalTo("Students[0].AccountNumber"))

上述内容显然不起作用,但这就是我想要比较的方式。基本上将密钥与另一个密钥进行比较,它们应该匹配。

这可行吗?

【问题讨论】:

    标签: java testing rest-assured web-api-testing rest-assured-jsonpath


    【解决方案1】:

    一种方法是:

    String A = 
    given ().
    when().
    get/put/post({api/name})
    .extract()
    .path("Students[0].Course.AccountNumber");
    
    String B = 
    given ().
    when().
    get/put/post({api/name})
    .extract()
    .path("Students[0].AccountNumber");
    
    Assert.assertEquals(A, B);
    

    似乎这种解决方法是唯一的方法。

    【讨论】:

      【解决方案2】:

      请参阅放心文档的Use the response to verify other parts of the response 部分。您基本上想创建一个实现ResponseAwareMatcher<Response> 的 lambda。像这样的:

      get("/x").then().body("href", response -> equalTo("http://localhost:8080/" + response.path("userId"));
      

      【讨论】:

        猜你喜欢
        • 2012-02-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-08-25
        • 1970-01-01
        • 2012-01-11
        • 1970-01-01
        • 2017-03-21
        相关资源
        最近更新 更多