RestAssured的学习之路0-初识restassured

rest assured的中文翻译为:放心,嗯,使用它就可以让我们放心了:)


0. rest assured用来干嘛

REST Assured is a Java DSL for simplifying testing of REST based services built on top of HTTP Builder. It supports POST, GET, PUT, DELETE, OPTIONS, PATCH and HEAD requests and can be used to validate and verify the response of these requests.

大意就是用来验证各种requests请求:)


1. 接口测试

为什么很多人用它来做接口测试?我目前想的不是很清楚,以后可能会更清楚点吧。就我目前对它的了解,rest assured的确比较容易使用,例如:我想验证这个接口的data数据:
(https://api.datausa.io/api/?show=geo&sumlevel=nation&required=pop)

{“data”: [[2013, “01000US”, 311536594], [2014, “01000US”, 314107084], [2015, “01000US”, 316515021], [2016, “01000US”, 318558162]], “headers”: [“year”, “geo”, “pop”],
“source”: {“link”: “http://www.census.gov/programs-surveys/acs/“, “org”: “Census Bureau”, “table”: “acs_5yr.yg”, “supported_levels”: {“geo”: [“nation”, “state”, “county”, “msa”, “puma”, “place”, “tract”, “all”]}, “dataset”: “ACS 5-year Estimate”},
“subs”: {},
“logic”: [{“link”: “http://www.census.gov/programs-surveys/acs/“, “org”: “Census Bureau”, “table”: “acs_5yr.yg”, “supported_levels”: {“geo”: [“nation”, “state”, “county”, “msa”, “puma”, “place”, “tract”, “all”]}, “dataset”: “ACS 5-year Estimate”}, {“link”: “http://www.census.gov/programs-surveys/acs/“, “org”: “Census Bureau”, “table”: “acs_1yr.yg”, “supported_levels”: {“geo”: [“nation”, “state”, “county”, “msa”, “place”, “all”]}, “dataset”: “ACS 1-year Estimate”}]


第一种写法是这样的:

get("https://api.datausa.io/api/?show=geo&sumlevel=nation&required=pop").then().body("data[0][0]",equalTo(2013));

第二种写法是这样的:

request("get","https://api.datausa.io/api/?show=geo&sumlevel=nation&required=pop").then().body("data[0][0]",equalTo(2013));

是不是有点感觉了(zhen mei you)!


2. 正式开始学(fan)习(yi)了

2.1 静态导入

为了更有效率的使用REST assured,以下类推荐静态导入

io.restassured.RestAssured.*
io.restassured.matcher.RestAssuredMatchers.*
org.hamcrest.Matchers.*

如果你需要使用Json Schema,以下类也推荐静态导入

io.restassured.module.jsv.JsonSchemaValidator.*

参考Json Schema获取更多使用信息(暂时未翻译)

如果你在使用Spring MVC,你可以使用spring-mock-mvc模块来对你的控制器进行单元测试,为了使用它的方法,你得先静态导入RestAssuredMockMvc而不是io.restassured.RestAssured

io.restassured.module.mockmvc.RestAssuredMockMvc.*

2.2 实例

假设以下的get请求返回的json个是数据如下:

{
"lotto":{
 "lottoId":5,
 "winning-numbers":[2,45,34,23,7,5,3],
 "winners":[{
   "winnerId":23,
   "numbers":[2,45,34,23,3,5]
 },{
   "winnerId":54,
   "numbers":[52,3,12,11,18,22]
 }]
}
}

REST assured能够轻松的帮你发出get请求和验证返回结果,例如,如果你想验证lottoId的值为5,那么,你可以这么做:

get("/lotto").then().body("lotto.lottoId", equalTo(5));

或者你想验证winnerId的值,你可以这么做:

get("/lotto").then().body("lotto.winners.winnerId", hasItems(23, 54));

注意:equalTo、hasItems是Hamcrest matchers的方法,因此,你需要静态导入下面的类

org.hamcrest.Matchers

2.3 返回值为浮点数如BigDecimal

备注:这里的json path使用的是Groovy’s Gpath,不要与Jayway的jsonpath搞混了

你可以使用rest assured的配置和jsonpath来限制(强制转换)请求返回的数据为BigDecima,而不是float或者double,比如,接口返回

"price":12.12 

一般情况下,你可以这么验证

get("/price").then().body("price", is(12.12f));

但是你可以使rest assured使用jsonconfig,这样就可以限制所有json返回的数字都是BigDecima

given().
        config(RestAssured.config().jsonConfig(jsonConfig().numberReturnType(BIG_DECIMAL))).
when().
        get("/price").
then().
        body("price", is(new BigDecimal(12.12));

2.4.1 json schema验证

从rest assured 2.1.0版本开始支持json schema验证,假设将下面的json schema以products-schema.json放到classpath

{
    "$schema": "http://json-schema.org/draft-04/schema#",
    "title": "Product set",
    "type": "array",
    "items": {
        "title": "Product",
        "type": "object",
        "properties": {
            "id": {
                "description": "The unique identifier for a product",
                "type": "number"
            },
            "name": {
                "type": "string"
            },
            "price": {
                "type": "number",
                "minimum": 0,
                "exclusiveMinimum": true
            },
            "tags": {
                "type": "array",
                "items": {
                    "type": "string" },
                "minItems": 1,
                "uniqueItems": true
            },
            "dimensions": {
                "type": "object",
                "properties": {
                    "length": {"type": "number"},
                    "width": {"type": "number"},
                    "height": {"type": "number"} },
                "required": ["length", "width", "height"]
            },
            "warehouseLocation": {
                "description": "Coordinates of the warehouse with the product",
                "$ref": "http://json-schema.org/geo"
            }
        },
        "required": ["id", "name", "price"]
    }
}

然后你就可以使用schema来验证数据了

get("/products").then().assertThat().body(matchesJsonSchemaInClasspath("products-schema.json"));

matchesJsonSchemaInClasspath需要从io.restassured.module.jsv.JsonSchemaValidator静态导入,当然,推荐是将所有方法都导入

io.restassured.module.jsv.JsonSchemaValidator.*

2.4.2 json schema配置

TO BE CONTINUE

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值