接口测试 rest-assured 中 JsonPath 的使用

49875183 for rest-assured · 2016年12月13日 · 最后由 donly 回复于 2018年08月06日 · 2181 次阅读

接口测试过程中往往需要对返回响应体的结构以及各节点的属性进行判断及验证,JsonPath 很便捷的解决了从庞大的节点获取属性的问题。

了解一下JsonPathrest-assured中的使用。

package com.restassured.test;

import static io.restassured.RestAssured.get;
import static org.hamcrest.Matchers.equalTo;
import java.util.Iterator;
import java.util.List;
import io.restassured.path.json.JsonPath;
import io.restassured.response.Response;

public class test { 

    public static void main(String[] args){ 

        //发起一个接口请求
        String url = "https://club.app.autohome.com.cn/club_v7.5.0/club/jinghuatopic-pm1-p1-s20-b0.json";
        Response response =get(url);

        //如果返回的结构体是json格式的话,用jsonpath可以很方便的获取json中的各种数据       
        System.out.println(response.getBody().jsonPath().getString("result.list[1].bbsname"));

        //直接获取json结构体中的数据
        System.out.println(response.getBody().jsonPath().getString("returncode"));

        //也可以从json结构体中指定的根路径开始获取(当前指定result)
        JsonPath jsonPath = new JsonPath(response.asString()).setRoot("result");            

        //获取result下list列表中的值
        System.out.println(jsonPath.get("list.bbsname"));
        System.out.println(jsonPath.get("list.userpic"));       

        //获取list下所有userpic的值并验证链接是否有效
        List<String> imgurls = jsonPath.getList("list.userpic");
        for (Iterator iterator = imgurls.iterator(); iterator.hasNext();) {
            String string = (String) iterator.next();
            System.out.println(string );        
        }

    }

}

测试过程中经常需要对于返回体中的部分元素进行验证,我们可以利用 org.hamcrest.Matchers 提供的各种断言结合rest-assured一起使用,来帮助简化测试代码。

比如判断 returncode 的值是否与预期的值一致

response.then().body("returncode", equalTo(0));

判断 url 返回的 code 码是否为 200 等等

get(url).then().assertThat().statusCode(200);

https://testerhome.com/topics/6451 这篇帖子中已经列出了大部分常用断言方式

对 org.hamcrest.Matchers 感兴趣的同学可以参照 http://hamcrest.org/JavaHamcrest/javadoc/1.3/org/hamcrest/Matchers.html 官网 API,进行学习。

JsonPath感兴趣的同学想要更深入的了解可以参照 https://github.com/jayway/JsonPath

rest-assured 中XmlPath的使用与JsonPath大同小异,感兴趣的同学可以参照 https://blog.jayway.com/2013/04/12/whats-new-in-rest-assured-1-8/

如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
共收到 3 条回复 时间 点赞

请教楼主:
post 的数据,如果是 json 数据,应该怎么操作呢?我在官网上看到用 map 的.但是如果是复杂的 json,嵌套好几层的那种,应该怎么处理呢?

白虹李李 回复

List bookList = JsonPath.from(jsonString).getList("store.book", Book.class);
这个 应该是 List = JsonPath.from(jsonString).getList("store.book", Book.class); 我 QQ 群里回你了,应该是你吧😁

徐老师好,请教一下这个函数的用法啊,多谢多谢。
List getList(String path, Class genericType)

就是官网的那个例子:
{
"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
}

能否使用 getList 方法,获取到一个 List呀?

这样会报错:
List bookList = JsonPath.from(jsonString).getList("store.book", Book.class);

Cannot convert class java.util.HashMap to class Book.

需要 登录 后方可回复, 如果你还没有账号请点击这里 注册