• 笔记:Junit5+RestAssured接口测试


    之前只用过testNG+httpClient做过接口测试,相较于httpClient用各种封装,RestAssured语言更简洁且规范一些。

    git:https://github.com/rest-assured/rest-assured/blob/master/xml-path/src/test/java/io/restassured/path/xml/XmlPathTest.java

    参考:

    xml和json 的断言:

    https://github.com/rest-assured/rest-assured/wiki/Usage#example-1---json

    https://github.com/rest-assured/rest-assured/blob/master/xml-path/src/test/java/io/restassured/path/xml/XmlPathTest.java

    RestAssured基本语法格式尊崇 given()-when()-then()的风格。

        @Test
        public void testMp2(){
            given()
                    .queryParam("wd", "mp2")
             .when()
                    .get("http://www.baidu.com")
             .then()
                    .log().all()
                    .statusCode(200);
        }
    

      

    支持json返回的各种花式解析

    json如下:

    {
        "store": {
            "book": [
                {
                    "category": "reference",
                    "author": "Nigel Rees",
                    "title": "Sayings of the Century",
                    "price": 8.95
                },
                {
                    "category": "fiction",
                    "author": "Evelyn Waugh",
                    "title": "Sword of Honour",
                    "price": 12.99
                },
                {
                    "category": "fiction",
                    "author": "Herman Melville",
                    "title": "Moby Dick",
                    "isbn": "0-553-21311-3",
                    "price": 8.99
                },
                {
                    "category": "fiction",
                    "author": "J. R. R. Tolkien",
                    "title": "The Lord of the Rings",
                    "isbn": "0-395-19395-8",
                    "price": 22.99
                }
            ],
            "bicycle": {
                "color": "red",
                "price": 19.95
            }
        },
        "expensive": 10
    }
    

      

    断言方法如下:

     @Test
        public void testJson2(){
            given()
                    .when().get("http://172.26.240.167:8000/testerhome.json")
                    .then()
                    .log().all()
                    .statusCode(200)
    //                .body("lotto.lottoId",equalTo(5))
    //                .body("lotto.winners.winnerId",hasItems(23,54))
            .body("store.book.category",hasItems("reference","fiction"))
            .body("store.book.category",hasItems("reference"))
            .body("store.book[0].author",equalTo("Nigel Rees"))
            .body("store.book.find{book->book.price==8.95f}.author",equalTo("Nigel Rees"))
            .body("store.book.find{book->book.price==8.95f}.price",equalTo(8.95f))
            .body("store.book.findAll{book->book.price>=5&&book.price<=15}.price",hasItems(8.95f))
            .body("expensive",equalTo(10))
    //                不支持json的find
    //                .body("**.find{it.winnerId}",equalTo(23))
            ;
    //                    .body(hasXPath("//*[]"));
        }
    

      

    支持花式解析xml

    xml如下:

    <shopping>
    <category type="groceries">
    <item>
    <name>Chocolate</name>
    <price>10</price>
    </item>
    <item>
    <name>Coffee</name>
    <price>20</price>
    </item>
    </category>
    <category type="supplies">
    <item>
    <name>Paper</name>
    <price>5</price>
    </item>
    <item quantity="4">
    <name>Pens</name>
    <price>15.5</price>
    </item>
    </category>
    <category type="present">
    <item when="Aug 10">
    <name>Kathryn's Birthday</name>
    <price>200</price>
    </item>
    </category>
    </shopping>
    

     

    断言如下:

      @Test
        public void testXml(){
            given()
                    .when().get("http://172.26.240.167:8000/testerhome.xml")
                    .then()
                    .log().all()
                    .statusCode(200)
            .body("shopping.category.item[0].price",equalTo("10"))
            .body("shopping.category.item[0].name",equalTo("Chocolate"))
            .body("shopping.category.item.size()",equalTo(5))
            .body("shopping.category.findAll {it.@type=='groceries'}.size()",equalTo(1))
            .body("shopping.category.item.findAll {it.price==20}.name",equalTo("Coffee"))
            .body("**.findAll{it.price==20}.name",equalTo("Coffee"))
            ;
        }
    

      

    proxy设置

            RestAssured.proxy("127.0.0.1",8888);
            RestAssured.useRelaxedHTTPSValidation();//证书信任
            //请求中有中文
            RestAssured.config=RestAssured.config()
                    .encoderConfig(EncoderConfig.encoderConfig().defaultContentCharset("UTF-8"));
    

      

    同时给所有请求设置header,利用filter()

     

    RestAssured.filters((req,res,ctx)->{
                req.header("User-Agent","Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.121 Safari/537.36")
                        .header("Content-Type","application/x-www-form-urlencoded")
                        .header("Cookie","u=yindongzi");
                Response resOrigin = ctx.next(req,res);
                return resOrigin;
            });
    

      

    post 请求中发送json

        @Test
        public void testPostJson(){
            HashMap<String,Object> map =new HashMap<>();
            map.put("Firstname","jack");
            map.put("LastName","john");
            map.put("array",new String[]{"111","222","333"});
            given()
                    .contentType(ContentType.JSON)
                    .body(map)
                    .when().post("http://www.baidu.com")
                    .then().statusCode(200);
        }
    

      

    导出response,extract()方法,

     Response response=
                    given()
                            .when().post("https://www.baidu.com")
                            .then()
                            .extract().response();
            //cookie = response.getHeaders().getValue("Set-Cookie").split(";")[0];
            //String cookie2 = response.body().jsonPath().getString("data");
            
    

      

  • 相关阅读:
    深入分析java的clone
    java实现文件夹遍历
    插入排序的C语言实现
    java动态代理工厂类
    HTML 理解标签 帧
    贝塞尔曲线初探
    警告:未初始化的变量引起运行问题
    解释string类型的输入操作符和getline函数分别如何处理空白符
    程序书写规范笔记
    vector
  • 原文地址:https://www.cnblogs.com/zhizhiyin/p/12268159.html
Copyright © 2020-2023  润新知