JSON 个包含 Hamcrest 和 RestAssured 重复项的数组

JSON arrays with duplicate items with Hamcrest and RestAssured

作为我在

中的问题的后续问题

如何安心使用 hamcrest 进行测试

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

存在两种数据,一种包含mobilenum, firstname等outbound等于yes,另一种no。

这就像有两个具有相同属性的对象,除了出站 属性。

约翰对上一个问题的回答是这样的:

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

我不知道如何添加 withArgs("Allen") 和 ("Edwards) .equalTo("outbound")


更新

我希望发生的事情是这样的:

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

如果我的手机号码等于 +6519829340 并且是出境的,则断言该位置在新加坡并且计数为 15。

如果我理解正确(并且用户列表(?)被称为 smsentries,就像在上一个问题中一样),您可以这样做:

.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

澄清后更新

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

你可以这样做:

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