-
接口自动化的jar
httpclient
-
执行步骤
搭环境:
1.根据接口规范编剧写请求/响应javabean
基本类型:lnt,boolean
引用类型:String
集合
2.根据接口规范编写测试类
3.实例化请求javabean 进行设值
4.根据接口规范度设值红藕的javabean进行转换格式 调用JSON.toJSONSteing()方法
5.根据接口规范声明请求路径
6.调用httpTool工具类 电泳dopostjson方法传入请求接口路径 json数据内容 编码格式
7.声明变量保存执行接口请求后响应结果内容
8.调用响应javabean保存响应结果使用json转对象方法 JSON.parseObject()方法
9.添加断言.断言响应结果是否正确
-
实例步骤
private String accountName;
private int changeMoney;
public String getAccountName() {
return accountName;
}
public int getChangeMoney() {
return changeMoney;
}
public void setAccountName(String accountName) {
this.accountName = accountName;
}
public void setChangeMoney(int changeMoney) {
this.changeMoney = changeMoney;
}
@Override
public String toString() {
return "GetBillsRequest{" +
"accountName='" + accountName + '\'' +
", changeMoney=" + changeMoney +
'}';
}
}
private String respCode;
private String respDesc;
public String getRespCode() {
return respCode;
}
public String getRespDesc() {
return respDesc;
}
public void setRespCode(String respCode) {
this.respCode = respCode;
}
public void setRespDesc(String respDesc) {
this.respDesc = respDesc;
}
@Override
public String toString() {
return "GetBillsResponse{" +
"respCode='" + respCode + '\'' +
", respDesc='" + respDesc + '\'' +
'}';
}
}
-
顺序
image.png
@Test
public void GetBills(){
GetBillsRequest req= new GetBillsRequest();
req.setAccountName("yewx111");
req.setChangeMoney(99999);
String reqjson = JSON.toJSONString(req, true);
System.out.println(reqjson);
String response = HttpTools.doPostByJson("http://qa.guoyasoft.com:8080/user/recharge", reqjson, "UTF-8");
System.out.println(response);
GetBillsResponse res = parseObject(response, GetBillsResponse.class);
System.out.println(res);
Assert.assertEquals(res.getRespCode(),"0000","预期业务响应码不一致");
}
}
-
技术点
1.接口规范转javabean
2.javabean存接口数据
3.请求javaBean转json格式
4.HttpClient发送http请求数据
5.响应json转javaBean
6.用TestNG做响应
-
代码模板
javaBean转json
http发送请求
响应json转JavaBean
网友评论