如何用java解析并从restassuredapi的响应中得到一些验证值

r6l8ljro  于 2021-07-09  发布在  Java
关注(0)|答案(1)|浏览(372)

我开始学习restassured,并设法从服务器获取响应和数据。但我无法解析和验证值。对于我的研究,我正在尝试获取所有数据并解析和验证。如果你解释我如何做,我将非常高兴。这是我的密码:

@Test
    public void getRequest() {

            String  key ="key";
            String token= "token";

            String id ="idvalue";
            Response response = given().spec(new RequestSpecBuilder().setBaseUri("https://api.trello.com/1").setContentType(ContentType.JSON)
                    .build())
                    .log().all()
                    .when()
                    .queryParams("key", key,
                           "token",
                                 token)
                               .pathParam("id", id)
                   .get("/cards/{id}");
                   response.then().statusCode(200)
//.body("name", hasItems("value"));//I want to check all of variables actually.this line not working.

           }

Postman 的回复如下:

{
    "id": "607ff8e3dd2e81327e55c05d",
    "checkItemStates": [],
    "closed": false,
    "dateLastActivity": "2021-04-21T10:05:23.805Z",
    "desc": "",
    "descData": null,
    "dueReminder": null,
    "idBoard": "607eee0d9f781d012c9407c9",
    "idList": "607f1a4e92b92d8fa44e07b8",
    "idMembersVoted": [],
    "idShort": 14,
    "idAttachmentCover": null,
    "idLabels": [],
    "manualCoverAttachment": false,
    "name": "güncellemekarti2",
    "pos": 114688,
    "shortLink": "5NTDEAXD",
    "isTemplate": false,
    "cardRole": null,
    "dueComplete": false,
    "due": null,
    "email": null,
    "labels": [],
    "shortUrl": "https://trello.com/c/5NTDEAXD",
    "start": null,
    "url": "https://trello.com/c/5NTDEAXD/14-g%C3%BCncellemekarti2",
    "idMembers": [],
    "badges": {
        "attachmentsByType": {
            "trello": {
                "board": 0,
                "card": 0
            }
        },
        "location": false,
        "votes": 0,
        "viewingMemberVoted": false,
        "subscribed": false,
        "fogbugz": "",
        "checkItems": 0,
        "checkItemsChecked": 0,
        "checkItemsEarliestDue": null,
        "comments": 0,
        "attachments": 0,
        "description": false,
        "due": null,
        "dueComplete": false,
        "start": null
    },
    "subscribed": false,
    "idChecklists": [],
    "cover": {
        "idAttachment": null,
        "color": null,
        "idUploadedBackground": null,
        "size": "normal",
        "brightness": "dark",
        "idPlugin": null
    }
}
flvtvl50

flvtvl501#

hasitems()更适合于数组—在您的情况下,请尝试以下方法:

.body("name", is("güncellemekarti2"));

相关问题