请放心,使用 Gpath 查询会出现错误 "The parameter "..." was used but not defined. Define parameters using the JsonPath.params(...)"

Rest assured, using Gpath query gives an error "The parameter "..." was used but not defined. Define parameters using the JsonPath.params(...)"

我是 rest-assured 的新手,目前我正在研究它以便在我们的测试框架中实现它。

我面临的问题是从 REST 响应的 Json 数组中提取对象。

我正在使用的例子json:

{
    "MRData": {
        "xmlns": "http://ergast.com/mrd/1.4",
        "series": "f1",
        "url": "http://ergast.com/api/f1/2016/drivers.json",
        "limit": "30",
        "offset": "0",
        "total": "24",
        "DriverTable": {
            "season": "2016",
            "Drivers": [
                {
                    "driverId": "alonso",
                    "permanentNumber": "14",
                    "code": "ALO",
                    "url": "http://en.wikipedia.org/wiki/Fernando_Alonso",
                    "givenName": "Fernando",
                    "familyName": "Alonso",
                    "dateOfBirth": "1981-07-29",
                    "nationality": "Spanish"
                },
                {
                    "driverId": "bottas",
                    "permanentNumber": "77",
                    "code": "BOT",
                    "url": "http://en.wikipedia.org/wiki/Valtteri_Bottas",
                    "givenName": "Valtteri",
                    "familyName": "Bottas",
                    "dateOfBirth": "1989-08-28",
                    "nationality": "Finnish"
                }
            ]
        }
    }
}

到目前为止我尝试过的事情:

这个断言有效

RestAssured.rootPath = "MRData.DriverTable.Drivers";
given()
.when()
.get("http://ergast.com/api/f1/2016/drivers.json")
.then()
.assertThat()
.body("find { find { d -> d.driverId == 'alonso' }.code }.code", equalTo("ALO"));

但我正在尝试实际获取特定数组项的 Json

RestAssured.rootPath = "MRData.DriverTable.Drivers";
given()
.when()
.get("http://ergast.com/api/f1/2016/drivers.json")
.then()
.extract()
//.jsonPath().param("driverId", "alonso").get("find { d -> d.driverId == driverId }");
.path("find { d -> d.driverId == 'alonso' }");

两种方式都试过了(一种被注释掉了)。但是我得到一个错误:

"The parameter "driverId" was used but not defined. Define parameters using the JsonPath.params(...)" 

RestAssured.rootPath = "MRData.DriverTable.Drivers"; 仅适用于 body 期望。对于提取,您必须使用参数的完整路径,例如MRData.DriverTable.Drivers.find { it.@driverId == 'alonso' }