首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在Restassured测试中使用queryParams代替Json body

在Restassured测试中使用queryParams代替Json body
EN

Stack Overflow用户
提问于 2020-05-29 17:23:26
回答 1查看 57关注 0票数 1

我正在使用RestAssured库在Java语言中自动执行API响应,下面是我使用的API主体:

代码语言:javascript
复制
{



"meta": {
    "language": "en",
    "marketCountry": "IN",
    "sourceUrl": "https://cj-gaq-dev.logistics.dhl/regular-shipment.html?en-AU"
},
"contactInformation": {
    "company": "test",
    "firstName": "test",
    "lastName": "test",
    "address": "test",
    "zip": "test",
    "city": "test",
    "country": "IN",
    "countryDisplay": "India",
    "email": "test@test.com",
    "phoneNumber": "2324243243",
    "comments": "test"
},
"shipmentScale": {
    "domestic": false,
    "regional": false,
    "global": true
},
"shipmentProduct": {
    "FREIGHT": {
        "numberOfShipments": "50",
        "frequency": "WEEKLY",
        "checked": true
    }



}

我希望使用queryParameters,而不是使用整个api主体。有办法做到这一点吗?

这是我到目前为止一直在使用的,并且一直得到422状态码错误:

代码语言:javascript
复制
String result = given().header("Content-Type","application/json" )
            .header("Accept","application/json").log().all().queryParam("marketCountry", "IN").queryParam("shipmentScale.domestic", "false")
            .queryParam("shipmentScale.regional", "false").queryParam("shipmentScale.global", "true")
            .queryParam("shipmentProduct.FREIGHT.checked", "true")
            .queryParam("shipmentProduct.FREIGHT.numberOfShipments", "50")
            .queryParam("shipmentProduct.FREIGHT.frequency", "WEEKLY")
            .queryParam("contactInformation.company", "test")
            .queryParam("contactInformation.firstName", "test")
            .queryParam("contactInformation.lastName", "test")
            .queryParam("contactInformation.address", "test")
            .queryParam("contactInformation.zip", "test")
            .queryParam("contactInformation.city", "test")
            .queryParam("contactInformation.email", "test@test.com")
            .queryParam("contactInformation.phoneNumber", "213456")
            .queryParam("contactInformation.comments", "test")
            .queryParam("contactInformation.country", "IN")
            .queryParam("contactInformation.comments", "test")
            .when().post().then().assertThat().statusCode(200).extract().response().asString();
EN

回答 1

Stack Overflow用户

发布于 2021-02-10 08:31:30

一种简单的方法是使用RestAssured库自动执行Java语言中的应用程序接口响应。查看下面的java类:

代码语言:javascript
复制
public class Basics {

    public static void main(String[] args) {
        
        RestAssured.baseURI = "https://12.23.454.55";
        given().log().all().queryParam("key", "mykey123").header("Content-Type","application/json")
        .body("{\r\n" + 
                "\r\n" + 
                "\r\n" + 
                "\r\n" + 
                "\"meta\": {\r\n" + 
                "    \"language\": \"en\",\r\n" + 
                "    \"marketCountry\": \"IN\",\r\n" + 
                "    \"sourceUrl\": \"https://cj-gaq-dev.logistics.dhl/regular-shipment.html?en-AU\"\r\n" + 
                "},\r\n" + 
                "\"contactInformation\": {\r\n" + 
                "    \"company\": \"test\",\r\n" + 
                "    \"firstName\": \"test\",\r\n" + 
                "    \"lastName\": \"test\",\r\n" + 
                "    \"address\": \"test\",\r\n" + 
                "    \"zip\": \"test\",\r\n" + 
                "    \"city\": \"test\",\r\n" + 
                "    \"country\": \"IN\",\r\n" + 
                "    \"countryDisplay\": \"India\",\r\n" + 
                "    \"email\": \"test@test.com\",\r\n" + 
                "    \"phoneNumber\": \"2324243243\",\r\n" + 
                "    \"comments\": \"test\"\r\n" + 
                "},\r\n" + 
                "\"shipmentScale\": {\r\n" + 
                "    \"domestic\": false,\r\n" + 
                "    \"regional\": false,\r\n" + 
                "    \"global\": true\r\n" + 
                "},\r\n" + 
                "\"shipmentProduct\": {\r\n" + 
                "    \"FREIGHT\": {\r\n" + 
                "        \"numberOfShipments\": \"50\",\r\n" + 
                "        \"frequency\": \"WEEKLY\",\r\n" + 
                "        \"checked\": true\r\n" + 
                "    }\r\n" + 
                "\r\n" + 
                "\r\n" + 
                "\r\n" + 
                "}").when().post("api/add/json")
        .then().log().all().assertThat().statusCode(200);

    }

}

如果愿意,您可以对响应进行更多的验证,或者可以使用JsonPath类来解析您的json响应,以便进行进一步的验证。

然而,另一种方法是将json请求放在单独的class方法中,并在请求体中返回对象。或者更好的是,您可以探索POJO java类的可能性。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62082642

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档