3

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!

4
  • 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. Commented Jul 27, 2020 at 11:19
  • I have tried: find body(" find {it.id=='346'}.personDependencies.name", hasItems("json", "jackson")) but this returns a null Commented Jul 27, 2020 at 11:35
  • What exactly is your requirement ? Commented Jul 27, 2020 at 12:29
  • 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 Commented Jul 27, 2020 at 12:32

3 Answers 3

2

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"));
Sign up to request clarification or add additional context in comments.

Comments

1

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 Comments

"hasItems" is not causing problems.
And what is the problem?
I have to select a specific instance in my array for the one specific user as @ewramner suggested above, but I do not know how.
0

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"
    }
  ]
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.