首页 > 解决方案 > 以 ASC/DESC 顺序放心响应

问题描述

有什么方法可以按顺序验证响应吗?

"user": [
            {
                "_id": "1",
                "age": 21,             
            },
            {
                "_id": "2",
                "age": 22,
            },
            {
                "_id": "7",
                "age": 30,
            }
]

我可以通过以下方式获得特定的 id 值

 .body("user[0].id", some validation)  result: 1

当我做这样的事情时,我会得到所有的 id

.body("user.id", some validation)  result: [1,2,7]

需要验证以验证结果是否按 DESC/ASC 顺序排列

.body("user.id", isDescOrder()/isAscOrder())

标签: rest-assuredhamcrest

解决方案


您可以isInOrder(Iterable<? extends T> iterable, Comparator<T> comparator)com.google.common.collect.Comparators中使用。

List<String> user_IdList = given()
        .when()
        .get("api_url")
        .then()
        .extract()
        .jsonPath()
        .getList("user._id");

List<Integer> integerList = user_IdList
        .stream()
        .map(x-> Integer.parseInt(x))
        .collect(Collectors.toList());

boolean isInAscOrder = isInOrder(integerList, Ordering.natural());

isInAscOrder将是真实的。因为 1,2,7 是按升序排列的。


推荐阅读