2
votes

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!

2
See github.com/rest-assured/rest-assured/issues/805 for examples, you are dealing with an array and need to select a specific instance or do the assert for all instances. - ewramner
I have tried: find body(" find {it.id=='346'}.personDependencies.name", hasItems("json", "jackson")) but this returns a null - bioinformatic98
What exactly is your requirement ? - Wilfred Clement
As you can see, it is only one id in the output JSON file, but what I want to achieve is to be able to select one of the objects with for example id=400 and make assertion if that object has certain values in personDependencies. I have tried: find body(" find {it.id=='346'}.personDependencies.name", hasItems("json", "jackson")) but this returns a null - bioinformatic98

2 Answers

1
votes

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"));
1
votes

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"));