$不使用JsonPath

时间:2017-11-09 17:54:10

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

我使用RestAssured发布一个Json,然后我需要验证所有字段是否都使用正确的值存储在数据库中。我的Json是:

{
    "id": "1",
    "name": "name1",
    "description": "description1",
    "source": "source1",
    "target": "target1",
    "domain": "PM",
    "transformation_rules": [
        {
            "name": "name2",
            "filters": [
                {
                    "object": "object1",
                    "pattern": "pattern1"
                }
            ],
            "operations": [
                {
                    "pattern": "pattern2",
                    "replacement": "replacement1"
                }
            ]
        },
        {
            "name": "name3",
            "filters": [
                {
                    "object": "object2",
                    "pattern": "pattern2"
                }
            ],
            "operations": [
                {
                    "pattern": "pattern3",
                    "replacement": "replacement2"
                },
                {
                    "pattern": "pattern3",
                    "replacement": "replacement3"
                },
                {
                    "pattern": "pattern4",
                    "replacement": "replacement4"
                }
            ]
        }
    ],
    "conflict_policy": "ACCEPT_SOURCE"
}

所以,我有:

responseGet = RestAssured.given().contentType(ContentType.JSON).when().get(urlApi + "/" + id);

我的第一个验证是:

responseGet.then().body("$[0]['id']", equalTo("1"));

验证字段“id”是否等于1它没有很好地执行,我改为:

responseGet.then().body("$.id", equalTo("1"));

和相同的结果--->失败

拜托,您能否给我一些测试所有Json的建议? 仅供参考,我尝试申请:https://github.com/json-path/JsonPath

非常感谢您提前,

最诚挚的问候,

1 个答案:

答案 0 :(得分:0)

您可以直接使用 jsonPath() 进行检查:

例如:

responseGet.body().jsonPath().getString("id").equals("1");

阅读JsonPath