如何使用Rest Assured在相同的API主体上发送String []和String值?

时间:2019-07-17 13:26:03

标签: java api cucumber rest-assured

我正在尝试使用Rest-Assured和Java为POST API创建测试自动化。

此API的后继正文为Application / JSON:

{
    "billing_address": {
        "region": "Sao Paulo",
        "region_id": 123,
        "country_id": "BR",
        "street": ["Av Paulista"],
        "postcode": "10577",
        "city": "SAO PAULO",
        "telephone": "555-555-1111",
        "company": "Test test",
        "firstname": "Fernando",
        "lastname": "Alves",
        "document": "0123444064"
     }
}

在我的代码中,我开发了follow方法来发送此值和请求:

public void criarPF (String srtAmbiente, String srtAPI, String srtToken, String srtRegion, String srtRegionID, String srtCountryID, String srtStreet,
                    String srtPostcode, String srtCity, String srtTelephone, String srtCompany, String srtFirstname, String srtLastname, String srtDocument) {

                    String uriBase = srtAmbiente;
                    String Api = srtAPI;

                    Map<String, String> addressContent = new HashMap<String,String>();
                    addressContent.put("region", srtRegion);
                    addressContent.put("region_id", srtRegionID);
                    addressContent.put("country_id", srtCountryID);
                    addressContent.put("street", srtStreet);
                    addressContent.put("postcode", srtPostcode);
                    addressContent.put("city", srtCity);
                    addressContent.put("telephone", srtTelephone);
                    addressContent.put("company", srtCompany);
                    addressContent.put("firstname", srtFirstname);
                    addressContent.put("lastname", srtLastname);
                    addressContent.put("document", srtDocument);


                    Map<String, Object> postContent = new HashMap<String,Object>();
                    postContent.put("billing_address", addressContent);


                    request = RestAssured.given().contentType(ContentType.JSON)
                            .header("Authorization", "Bearer "+srtToken).with().body(postContent);

我收到了一个错误消息使者,“ street”不是String,而是String []。 因此,如何在相同的API主体中发送String和String []值。谢谢!

1 个答案:

答案 0 :(得分:2)

您正在将street设置为字符串值。只需这样更改即可:

Map<String, Object> addressContent = new HashMap<>();
addressContent.put("region", srtRegion);
addressContent.put("region_id", srtRegionID);
addressContent.put("country_id", srtCountryID);

// Here wrap strStreet in a List
addressContent.put("street", Arrays.asList(srtStreet));

addressContent.put("postcode", srtPostcode);
addressContent.put("city", srtCity);
addressContent.put("telephone", srtTelephone);
addressContent.put("company", srtCompany);
addressContent.put("firstname", srtFirstname);
addressContent.put("lastname", srtLastname);
addressContent.put("document", srtDocument);