一,请求引擎的封装
- 设计到遍历javamap的操作,遍历map的几种方法可参照下面这个连接博文
- https://blog.csdn.net/gaojiajie333/article/details/79353774 ```java package com.addicated.wework;
import io.restassured.response.Response; import io.restassured.specification.RequestSpecification;
import java.util.HashMap;
import static io.restassured.RestAssured.given;
/**
- @description: 请求引擎封装
- @author: Adi
- @time: 2020/10/5 11:28 **/
public class Restful {
HashMap
// 调用流程,先调用 query(map类型),将要传入的数据通过KV对应传入,// 之后 send内部方法运行,将query内部的请求参数通过键值对的形式压入 requestSpecification对象中// 之后直接通过requestSpecification对象来进行query.entrySet().forEach(entry->{requestSpecification.queryParam(entry.getKey(),entry.getValue());});return requestSpecification.when().request("get", "baidu.com");}
}
- 之后在contact目录下创建一个Contact类来继承Restful```javapackage com.addicated.wework.contact;import com.addicated.wework.Restful;/*** @description:* @author: Adi* @time: 2020/10/5 12:59**/public class Contact extends Restful {// 写一个构造方法,当该类被实例化调用的时候进行初始化public Contact() {reset();}// 实例化的时候执行reset方法,重新设置tokenpublic void reset() {requestSpecification = given().log().all().queryParam("access_token", Wework.getToken()).contentType(ContentType.JSON);}}
- 之后在Department类中继承Contact ```java package com.addicated.wework.contact;
import com.addicated.wework.Wework; import com.jayway.jsonpath.JsonPath; import io.restassured.response.Response;
import static io.restassured.RestAssured.given;
/**
- @description:
- @author: Adi
- @time: 2020/10/4 19:12 **/
public class Department extends Contact {
public Response list(String id) {// 直接调用requestSpectfication 来替代原先的封装逻辑Response response = requestSpecification.param("id", id).when().get("https://qyapi.weixin.qq.com/cgi-bin/department/list").then().extract().response();reset(); // 将map里面的数据清空return response;}// 创建部门public Response create(String name, String parentid) {reset();String body = JsonPath.parse(this.getClass().getResourceAsStream("/data/create.json")).set("$.name", name).set("parentid", parentid).jsonString();return requestSpecification.body(body).when().post("https://qyapi.weixin.qq.com/cgi-bin/department/create").then().log().all().statusCode(200).extract().response();}// 删除部门public Response delete(String id) {reset();return requestSpecification.queryParam("id", id).when().get("https://qyapi.weixin.qq.com/cgi-bin/department/delete").then().extract().response();}// 更新部门public Response update(String name, String id) {reset();String body = JsonPath.parse(this.getClass().getResourceAsStream("/data/update.json")).set("$.name", name).set("id", id).jsonString();return requestSpecification.body(body).when().post("https://qyapi.weixin.qq.com/cgi-bin/department/update").then().extract().response();}
}
<a name="Wgb6i"></a># 二,再优化- 现在的参数化是通过手动赋值,然后替换参数实现的,仍然有过多细节暴露在调用处,进一步进行优化,使用map格式进行参数化```java// 创建部门 参数化优化public Response createMap(HashMap<String, Object> map) {reset();// 首先拿到 读取json文件的对象并保存,之后进行赋值DocumentContext documentContext = JsonPath.parse(this.getClass().getResourceAsStream("/data/create.json"));// 这种书写方式的有点是,传值可以传一个也可多可,泛用性更强map.entrySet().forEach(entry -> {documentContext.set(entry.getKey(), entry.getValue());});return requestSpecification.body(documentContext.jsonString()).when().post("https://qyapi.weixin.qq.com/cgi-bin/department/create").then().log().all().statusCode(200).extract().response();}------------- 测试方法中@Testvoid createByMap() {HashMap<String, Object> map = new HashMap<String, Object>() {{put("name", String.format("seveniruby_d1_map%s", random));put("parentid", "1");}};department.createMap(map).then().body("errcode", equalTo(0));}// 直接通过hashmap传值即可
