带有 Hamcrest 和 RestAssured 重复项的 JSON 数组 [英] JSON arrays with duplicate items with Hamcrest and RestAssured

查看:34
本文介绍了带有 Hamcrest 和 RestAssured 重复项的 JSON 数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

作为我在 处理带有 hamcrest 和 rest 的数组的问题的后续问题放心

我怎样才能放心使用 hamcrest 以便我可以测试

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
}

存在两种类型的数据,一种包含 mobilenum、firstname 等,outbound 等于 yes,另一种为 no.

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

我不知道如何添加类似 withArgs("Allen") 和 ("Edwards) .equalTo("outbound") 的内容

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

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

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

如果我的手机号码等于 +6519829340 并且是出站号码,请断言该位置在新加坡并且计数为 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.

推荐答案

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

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

澄清后更新

如果我的手机号码等于 +6519829340 并且是出站的,则断言该位置在新加坡并且计数为 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.

你可以这样做:

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

这篇关于带有 Hamcrest 和 RestAssured 重复项的 JSON 数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆