JSON arrays with duplicate items with Hamcrest and RestAssured

我的未来我决定 提交于 2019-12-12 04:05:43

问题


As a follow up question for my question in Dealing arrays with hamcrest and rest assured

How can I use hamcrest with restassured so that I can test

{
    "mobilenum": "+6519829340",
    "firstname": "Allen",
    "lastname": "Edwards",
    "location": "Singapore"
    "outbound": "YES"
    "count" : 15
}, 
{
    "mobilenum": "+6519829340",
    "firstname": "Allen",
    "lastname": "Edwards",
    "location": "Singapore"
    "outbound": "NO"
    "count" : 9
}

That there exist two types of data, one containing mobilenum, firstname, etc having outbound equal to yes, and the other no.

It would be like having two objects having the same properties except the outbound property.

An answer by John, from the previous question is this:

 .root("smsentries.find { it.mobilenum == '%s' }").    
 .body("firstname", withArgs("+6519829340"), equalTo("Allen")
 .body("lastname", withArgs("+6519829340"), equalTo("Edwards").
 .body("firstname", withArgs("+12345678"), equalTo("John")
 .body("lastname", withArgs("+12345678"), equalTo("Doe").

I don't know how to add something like withArgs("Allen") and ("Edwards) .equalTo("outbound")


UPDATE

What I hope to happen is like this:

 for (Map.Entry<String,JsonElement> entry : o.entrySet()) {
        if (entry.getKey().equals("smsentries")) {
        JsonArray array = entry.getValue().getAsJsonArray();
        for (JsonElement elementJSON : array) {
            SMSEntry smsEntry = mapper.readValue(elementJSON.toString(), SMSEntry.class);
                if (smsEntry.getMobilenum().equals("+6519829340") && 
                        smsEntry.getOutbound().equals("YES")) {
                            assertThat(smsEntry.getLocation(), equalTo("Singapore"));
                            assertThat(smsEntry.getCount(), equalTo(15));
                        }
            }
        }
    }

If I have a mobile number equal to +6519829340 and is outbound, assert that the location is in Singapore and has count of 15.


回答1:


If I understand you correctly (and that the list of users(?) is called smsentries as it was in the previous question) you could do like this:

.root("smsentries.findAll { it.mobilenum == '%s' }").    
.body("firstname", withArgs("+6519829340"), contains("Allen", "Allen"))
.body("lastname", withArgs("+6519829340"), contains("Edwards", "Edwards"))
.body("outbound", withArgs("+6519829340"), containsInAnyOrder("YES", "NO"))
// Additional body matchers

Update after clarification

If I have a mobile number equal to +6519829340 and is outbound, assert that the location is in Singapore and has count of 15.

You can do like this:

.root("smsentries.find { it.mobilenum == '+6519829340' && it.outbound == 'YES'}").    
.body("location", equalTo("Singapore"))
.body("count", equalTo(9))


来源:https://stackoverflow.com/questions/30706158/json-arrays-with-duplicate-items-with-hamcrest-and-restassured

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!