meghaclr
meghaclr

Reputation: 47

Karate match each on response assertion is failing to identify missing keys in response

I have a match each assertion like below in my code. Just tried creating similar examples as my code, just to explain the issue.

  Scenario: Example scenario 1

    * def response =
"""
[
    {
        id: 1,
        name: "John",
        password: "abc123"
    },
    {
        id: 2,
        name: "David",
        password: "abc123"
    },
    {
        id: 3,
        name: "Mike",
        password: "abc123"
    },
    {
        id: 4,
        name: "Johny"
    }
]
"""

    * match each response[*].password contains 'abc123'

Test status : Pass

Password field is missing in object 4(where id=4). Above test is passing for me. I am expecting Karate to fail the test in this case. How can I make my test fail in this case?

  Scenario: Example scenario 2

    * def response =
"""
[
    {
        id: 1,
        name: "John",
    },
    {
        id: 2,
        name: "David",
    },
    {
        id: 3,
        name: "Mike",
    },
    {
        id: 4,
        name: "Johny"
    }
]
"""

    * match each response[*].password contains 'abc123'

Test status : Pass

Here, there is no password field at all in response. But my test is passing.

Need a work around to fail these kind of scenarios.

Example 3 :

    * def response =
"""
[
    {
        id: 1,
        name: "John",
        password: "abc123",
        skills :[ "training", "management"
        ]
    },
    {
        id: 2,
        name: "David",
        password: "abc123",
        skills :[ "training", "management"
        ]
    },
    {
        id: 3,
        name: "David",
        password: "abc123",
        skills :[ "training", "coding"
        ]
    },
    {
        id: 4,
        name: "David",
        password: "abc123",
        skills :[ "training", "management"
        ]
    }
]
"""

Considering * match each response contains { password: 'abc123' } format(mentioned by @peter) to check example 1 and 2, what if I want to check skills array having 'training' in each object under response? How can I achieve this?

Upvotes: 1

Views: 759

Answers (2)

Peter Thomas
Peter Thomas

Reputation: 58058

Note that response[*].password is a JsonPath expression that will return an array of all the password key-values found and will return only 3 in your case.

What you are looking for is this:

* match each response contains { password: 'abc123' }

Upvotes: 1

Vaibhav Khandelwal
Vaibhav Khandelwal

Reputation: 51

you can use match each to validate the json schema https://github.com/intuit/karate#match-each

Upvotes: 2

Related Questions