使用Rest Assured检查JSON数组的值时出现问题。

huangapple go评论72阅读模式
英文:

Problem with checking values of JSON array using Rest Assured

问题

我正在尝试使用Rest-Assured为我的应用程序添加一些测试,但我无法弄清楚如何断言一些嵌套值。错误消息如下:

期望:(包含“json”的集合)
实际:[[json,spring,gulp,path等...]]

以下是代码:

 when().
       get("/api/personsByID/{id}/{count}", 262, 2).
       then().
       statusCode(200).
       body("personDependencies.name", hasItems("json"));

以下是由rest控制器返回的JSON文件:

[
    {
        "id": 346,
        "verified": true,
        "displayName": "eda656a2c3cb59ae840e40a28ba4ab50bfb9de0185abcb901c6af6dc59d6668f",
        "emails": [
            {
                "email": "16a23f2e5477df0bbcad718c3abc235b2cb8a1b6648d14f58d42a7be13df2b6e"
            }
        ],
        "personDependencies": [
            {
                "name": "json"
            },
            {
                "name": "spring"
            },
            {
                "name": "gulp"
            },
            {
                "name": "path"
            },
            ...
            // 其他项
            ...
            {
                "name": "javax.servlet"
            }
        ],
        "countries": [],
        "member_of": [],
        "projects": [],
        ...
        // 其他项
        ...
        "distance": 0.6028837702084446
    }
]

我不知道如何进行适当的断言,任何帮助都将是很好的。谢谢!

英文:

I'm trying to add some tests with Rest-Assured to my application, but I can't figure out how to assert some nested values. The error message is :
>Expected: (a collection containing "json")
Actual: [[json, spring, gulp, path etc...]]

Here is the code :

 when().
       get("/api/personsByID/{id}/{count}", 262, 2).
       then().
       statusCode(200).
       body("personDependencies.name", hasItems("json"));

And here is the JSON file that is returned by rest controller:

[
    {
        "id": 346,
        "verified": true,
        "displayName": "eda656a2c3cb59ae840e40a28ba4ab50bfb9de0185abcb901c6af6dc59d6668f",
        "emails": [
            {
                "email": "16a23f2e5477df0bbcad718c3abc235b2cb8a1b6648d14f58d42a7be13df2b6e"
            }
        ],
        "personDependencies": [
            {
                "name": "json"
            },
            {
                "name": "spring"
            },
            {
                "name": "gulp"
            },
            {
                "name": "path"
            },
            {
                "name": "junit"
            },
            {
                "name": "activemq"
            },
            {
                "name": "hibernate"
            },
            {
                "name": "jstl"
            },
            {
                "name": "phantomjs"
            },
            {
                "name": "activiti"
            },
            {
                "name": "commons"
            },
            {
                "name": "h2"
            },
            {
                "name": "joda"
            },
            {
                "name": "log4j"
            },
            {
                "name": "exec"
            },
            {
                "name": "admin"
            },
            {
                "name": "coveralls"
            },
            {
                "name": "cxf"
            },
            {
                "name": "cglib"
            },
            {
                "name": "camel"
            },
            {
                "name": "sugaronrest"
            },
            {
                "name": "tslint"
            },
            {
                "name": "httpclient"
            },
            {
                "name": "guava"
            },
            {
                "name": "inventory"
            },
            {
                "name": "jackson"
            },
            {
                "name": "gson"
            },
            {
                "name": "event"
            },
            {
                "name": "OTRS"
            },
            {
                "name": "maven"
            },
            {
                "name": "karma"
            },
            {
                "name": "slf4j"
            },
            {
                "name": "postgresql"
            },
            {
                "name": "typescript"
            },
            {
                "name": "jasmine"
            },
            {
                "name": "spa"
            },
            {
                "name": "javax.servlet"
            }
        ],
        "countries": [],
        "member_of": [],
        "projects": [],
        "employee_type": [],
        "languages": [
            {
                "language": "reStructuredText",
                "sum": 575
            },
            {
                "language": "JSON",
                "sum": 21
            },
            {
                "language": "JavaScript",
                "sum": 4467
            },
            {
                "language": "Java",
                "sum": 7958
            },
            {
                "language": "Python",
                "sum": 2
            },
            {
                "language": "XML",
                "sum": 477
            },
            {
                "language": "Plain Text",
                "sum": 41
            }
        ],
        "distance": 0.6028837702084446
    }
]

I have no idea how to make proper assertions, any help would be great. Thanks!

答案1

得分: 2

如果我理解您的问题正确,您需要检查特定值是否存在于返回给定ID的列表中。

以下内容适用于您:

given().when().get().then().body("find {it.id == 346}.personDependencies.name", hasItems("json", "jackson"));
英文:

If I am reading your question right you need to check if a certain values are present in a list that is returned for a particular ID

The below should work for you

given().when().get().then().body("find {it.id == 346}.personDependencies.name", hasItems("json", "jackson"));

答案2

得分: 1

第一个问题是你不需要使用hasItems来检查项目的存在,应该使用hasItem

when().
       get("/api/personsByID/{id}/{count}", 262, 2).
       then().
       statusCode(200).
       body("personDependencies.name", hasItem("json"));

然后,如果在测试失败时需要向断言添加更多消息,你可以这样做:

when().
        get("/api/personsByID/{id}/{count}", 262, 2).
        then().
        statusCode(200).
        body("personDependencies.name",  describedAs("数组不包含所提供的项", hasItem("json")));

在你的情况下,你可以这样验证:

when().
       get("/api/personsByID/{id}/{count}", 262, 2).
       then().
       statusCode(200).
       body("personDependencies[*].name", hasItem("json"));
英文:

The first problem you don't need to check the presence of an item with hasItems, you should use hasItem

when().
       get("/api/personsByID/{id}/{count}", 262, 2).
       then().
       statusCode(200).
       body("personDependencies.name", hasItem("json"));

Then if you need to add more message to the assertion when the test fails you can do such way:

 when().
        get("/api/personsByID/{id}/{count}", 262, 2).
        then().
        statusCode(200).
        body("personDependencies.name",  describedAs("Array not containing the provided item",hasItem("json")));

In your case you can validate such a way:

when().
       get("/api/personsByID/{id}/{count}", 262, 2).
       then().
       statusCode(200).
       body("personDependencies[*].name", hasItem("json"));

答案3

得分: 0

Wilfred Clement的回答非常好。只想再添加一个例子以更好地理解概念和语法。
如果您必须在某个不在JSON的'root'级别上的数组中进行类似的搜索,请使用以下语法:

.body("problems.find {it.property == 'boardUrl'}.details", is("size must be between 1 and 256"))

此路径适用于以下JSON结构:

{
"title": "Bad Request",
"status": 400,
"problems": [
    {
        "property": "tag",
        "value": "too-big-tag-value",
        "details": "size must be between 1 and 10"
    },
    {
        "property": "name",
        "value": "Very big name for testing purposes",
        "details": "size must be between 1 and 20"
    }
  ]
}
英文:

Wilfred Clement's answer is great. Just want to add 1 more example for better understanding concept and syntax.
Use next syntax if you have to make similar search in some array which is not on 'root' level of JSON:

.body("problems.find {it.property == 'boardUrl'}.details", is("size must be between 1 and 256"))

This path is created for the JSON structure below:

{
"title": "Bad Request",
"status": 400,
"problems": [
    {
        "property": "tag",
        "value": "too-big-tag-value",
        "details": "size must be between 1 and 10"
    },
    {
        "property": "name",
        "value": "Very big name for testing purposes",
        "details": "size must be between 1 and 20"
    }
  ]
}

huangapple
  • 本文由 发表于 2020年7月27日 19:05:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/63114006.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定