博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
REST-assured 2发送消息代码重构
阅读量:4450 次
发布时间:2019-06-07

本文共 3834 字,大约阅读时间需要 12 分钟。

将获取token的方法封装到公共类

#javapackage date811;import io.restassured.response.Response;import org.testng.annotations.Test;import static org.junit.Assert.assertNotNull;import static org.junit.Assert.assertThat;import static io.restassured.RestAssured.given;public class WXUtil {    /*    公共类     */    private static String corpID = "xxxxx";    private static String corpSecret = "xxxxxxxx";    private static String tokenURL = "https://qyapi.weixin.qq.com/cgi-bin/gettoken";    //获取access_token的方法    public static String getToken(){        Response res = given().param("corpid",corpID).                param("corpsecret",corpSecret).get(tokenURL).prettyPeek();        String token_text = res.getBody().jsonPath().getString("access_token");        assertNotNull(token_text);        return token_text;    }}

将返回的body封装,便于修改和查看

#javapackage date811;import java.util.Map;public class WXTextMessage {    /**     *     {     *        "touser" : "UserID1|UserID2|UserID3",     *        "toparty" : "PartyID1|PartyID2",     *        "totag" : "TagID1 | TagID2",     *        "msgtype" : "text",     *        "agentid" : 1,     *        "text" : {     *            "content" : "你的快递已到,请携带工卡前往邮件中心领取。\n出发前可查看邮件中心视频实况,聪明避开排队。"     *        },     *        "safe":0     *     }     */    private String touser;    private String toparty;    private String totag;    private String msgtype;    private Integer agentid;    private Map
text; private Integer safe; /** * 生成set和get方法 * 右键选择Generate或者control+enter,Getter and Setter全选 */ public String getTouser() { return touser; } public void setTouser(String touser) { this.touser = touser; } public String getToparty() { return toparty; } public void setToparty(String toparty) { this.toparty = toparty; } public String getTotag() { return totag; } public void setTotag(String totag) { this.totag = totag; } public String getMsgtype() { return msgtype; } public void setMsgtype(String msgtype) { this.msgtype = msgtype; } public Integer getAgentid() { return agentid; } public void setAgentid(Integer agentid) { this.agentid = agentid; } public Map
getText() { return text; } public void setText(Map
text) { this.text = text; } public Integer getSafe() { return safe; } public void setSafe(Integer safe) { this.safe = safe; }}

执行类

#javapackage date811;import io.restassured.RestAssured.*;import io.restassured.http.ContentType;import io.restassured.matcher.RestAssuredMatchers.*;import io.restassured.response.Response;import org.hamcrest.Matchers.*;import org.testng.annotations.Test;import java.util.HashMap;import java.util.Map;import static io.restassured.RestAssured.given;import static org.hamcrest.Matchers.equalTo;//assertThat方法一定要静态导入import static org.hamcrest.Matchers.notNullValue;import static org.junit.Assert.assertThat;public class GetToken {    @Test    public void post_message() {        String token_text = WXUtil.getToken();        String postURL = "https://qyapi.weixin.qq.com/cgi-bin/message/send";        WXTextMessage wx = new WXTextMessage();        wx.setAgentid(0);        wx.setSafe(0);        wx.setToparty("0");        wx.setMsgtype("text");        Map
text = new HashMap<>(); text.put("content","你的快递已到,请携带工卡前往邮件中心领取。\\n" + "出发前可查看
" + "邮件中心视频实况,聪明避开排队"); wx.setText(text); Response res = given().contentType(ContentType.JSON).body(wx).queryParam("access_token",token_text) .post(postURL).prettyPeek(); assertThat(res.getBody().jsonPath().getString("errmsg"),equalTo("ok")); }}

1418970-20180811201409512-2031899627.png

转载于:https://www.cnblogs.com/csj2018/p/9460925.html

你可能感兴趣的文章
vue-cli3 中console.log报错
查看>>
GridView 中Item项居中显示
查看>>
UML类图五种关系与代码的对应关系
查看>>
如何理解作用域
查看>>
从无到满意offer,你需要知道的那些事
查看>>
P1516 青蛙的约会 洛谷
查看>>
SDOI2011 染色
查看>>
HTTP协议详解
查看>>
JQuery EasyUI combobox动态添加option
查看>>
面向连接的TCP概述
查看>>
前端快捷方式 [记录]
查看>>
亲测可用,解决端口被占用的指令!!
查看>>
MySQL--视图、触发器、事务、存储过程、内置函数、流程控制、索引
查看>>
Django--登录功能
查看>>
GitHub and Git
查看>>
Django--数据库查询操作
查看>>
自定义配置文件的使用
查看>>
js-20170609-运算符
查看>>
ALV弹出窗口&nbsp;&nbsp;&nbsp;REU…
查看>>
算法笔记_065:分治法求逆序对(Java)
查看>>