Github地址:https://github.com/rest-assured/rest-assured
使用企业微信API实现下列功能
▲ 获取Token
▲ 创建部门
▲ 修改部门
▲ 查询部门
▲ 删除部门
1.创建企业微信账号
2.查看API
登录企业微信,点击上方API文档 → 第三方应用开发 → 服务端API
▲ 获取Token
进入服务端API页后,默认就是快速入门,点击左开始开发 → 服务商的token
corpid
获取地址:我的企业 → 企业信息
provider_secret
获取地址:管理工具 → 通讯录同步
▲ 部门管理
服务端API → 通讯录管理 → 部门管理
参考PageObject设计模式,将功能模块设计为对象,用例只进行维护数据和断言,功能模块负责请求操作。
通过Maven获取三方框架(Pom.xml)
4.0.0
Testing
RequestDemo
1.0-SNAPSHOT
org.apache.maven.plugins
maven-compiler-plugin
1.8
UTF-8
org.apache.maven.plugins
maven-surefire-plugin
3.0.0-M4
org.junit.jupiter
junit-jupiter-engine
5.5.2
io.rest-assured
rest-assured
4.3.0
test
org.junit.jupiter
junit-jupiter-engine
5.5.2
test
org.junit.platform
junit-platform-runner
1.5.2
test
com.fasterxml.jackson.core
jackson-databind
2.11.0
在BaseRequest类中将获取Token和请求的重复代码进行封装,Request类继承后直接调用即可。
package com.testing.demo.request;
import io.restassured.http.ContentType;
import io.restassured.response.Response;
import io.restassured.specification.RequestSpecification;
import java.util.HashMap;
import java.util.Map;
import static io.restassured.RestAssured.given;
import static org.junit.jupiter.api.Assertions.*;
public class BaseRequest {
public static String token = ""; //获取一次后就不在获取
public String baseurl = "https://qyapi.weixin.qq.com/cgi-bin";
//获取tokenURL
public String getToken(){
if("".equals(token)){ //判断Token是否已经获取
Response response = given()
.queryParams(initTokenParameter()) //输入参数
.when()
.get(baseurl+ "/gettoken")
.then()
.statusCode(200) //判断响应码
.extract()
.response(); //获取响应
token = response.path("access_token"); //获取Token
assertTrue(token.length()>0); //判断token长度是否大于0
}
return token;
}
//初始化Token参数
private HashMap initTokenParameter() {
HashMap map = new HashMap();
map.put("corpid","XXXXXXXXXXXX");
map.put("corpsecret","XXXXXXXXXXXXX");
return map;
}
//Get请求
protected Response getRequest(String url, Map map){
return given()
.queryParams("access_token", getToken())
.queryParams(map)
.contentType(ContentType.JSON)
.when().log().all()
.get(baseurl + url)
.then()
.statusCode(200)
.extract()
.response();
}
//Post请求
protected Response postRequest(String url, Map map){
return given()
.queryParams("access_token", getToken())
.contentType(ContentType.JSON)
.body(map)
.when().log().all()
.post(baseurl + url)
.then()
.statusCode(200)
.extract()
.response();
}
}
package com.testing.demo.request;
import io.restassured.response.Response;
import java.util.HashMap;
import java.util.Map;
public class DepartmentRequest extends BaseRequest {
//创建部门
public Response create(String name, int parentid) {
//请求体数据
Map map = new HashMap();
map.put("name", name);
map.put("parentid", parentid);
String url = "/department/create";
return postRequest(url,map); //返回响应数据
}
//删除部门
public Response delete(int id) {
Map map = new HashMap();
map.put("id", id);
String url = "/department/delete";
return getRequest(url,map);
}
//更新部门
public Response update(String name, int id) {
Map map = new HashMap();
map.put("id", id);
map.put("name", name);
String url = "/department/update";
return postRequest(url,map);
}
//查询部门
public Response search(int id) {
Map map = new HashMap();
map.put("id", id);
String url = "/department/list";
return getRequest(url,map);
}
}
package com.testing.demo.testcase;
import com.testing.demo.request.DepartmentRequest;
import io.restassured.response.Response;
import org.junit.jupiter.api.*;
import static org.hamcrest.CoreMatchers.is;
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
public class TestCaseDepartment {
private static DepartmentRequest department;
private static int id;
private String name;
@BeforeAll
public static void BeaforeAll(){
department = new DepartmentRequest();
}
@Test
@Order(100)
public void createDepartment(){
int parentid = 1;
String name = "河南测试中心T";
Response response = department.create(name, parentid);
id = response.path("id");
System.out.println(id);
response.then().assertThat().body("errcode",is(0));
}
@Test
@Order(200)
public void updateDepartment(){
name = "北京测试中心T1";
Response response = department.update(name,id);
response.then().assertThat().body("errcode",is(0));
department.search(id).then().body("department[0].name",is(name));
}
@Test
@Order(300)
public void searchDepartment(){
Response response = department.search(id);
response.then().assertThat().body("errcode",is(0));
}
@Test
@Order(400)
public void deleteDepartment(){
Response response = department.delete(id);
response.then().assertThat().body("errcode",is(0));
}
}
上述代码中的断言写的比较简单,在实际工作中可以根据实际场景灵活应用JsonPath、Hamcrest、JsonSchame来完成断言。