【问题标题】:RestAssured Json response verification with GPath使用 GPath 进行 RestAssured Json 响应验证
【发布时间】:2019-08-26 16:00:33
【问题描述】:

我收到以下针对我的 API 的错误请求的响应。我将 RestAssured 用于我的休息响应断言。

{
    "message": "An entity of type topic was passed in an invalid format",
    "meta": {
        "display": {
            "topic": [
                {
                    "name": [
                        "must not be blank"
                    ]
                },
                {
                    "contentType": [
                        "must not be blank"
                    ]
                },
                {
                    "content": [
                        "must not be blank"
                    ]
                },
                {
                    "version": [
                        "must not be blank"
                    ]
                }
            ]
        }
    }
}

我需要验证响应的所有属性的值。我正在努力验证这条路径:meta.display.topic.contentType。我想不出它的 GPath。

这是我的断言:

given().body("{}").when()
            .post(BASE_URL)
            .prettyPeek()
            .then()
            .statusCode(400)
            .contentType(ContentType.JSON)
            .body("message", is("An entity of type topic was passed in an invalid format"),
                    "meta.display.topic.contentType", is("must not be blank"));

由于路径不正确,断言总是失败。

【问题讨论】:

  • 从您的示例响应 Gpath 应该是 meta.display.topic[1].contentType[0]
  • 我不想在主题对象内硬编码数组位置,有没有办法避免这样做?

标签: rest-assured rest-assured-jsonpath gpath


【解决方案1】:

为避免 topic 对象的硬编码数组位置,以下将起作用:

meta.display.topic.find { it.contentType != null }.contentType[0]

【讨论】:

  • 它可以工作,但我不想在我的代码中撒上 Groovy 脚本,即使是很小的脚本,我会假设会有一些路径匹配器或其他东西,这会促进这样的事情。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-07-09
  • 1970-01-01
  • 1970-01-01
  • 2018-01-02
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多