zoukankan      html  css  js  c++  java
  • 【Httpclient+TestNG】接口自动化测试——GET请求示例

      1 import net.sf.json.JSONArray;
      2 import net.sf.json.JSONObject;
      3 import org.apache.http.client.config.RequestConfig;
      4 import org.apache.http.client.methods.CloseableHttpResponse;
      5 import org.apache.http.client.methods.HttpGet;
      6 import org.apache.http.client.utils.URIBuilder;
      7 import org.apache.http.impl.client.CloseableHttpClient;
      8 import org.apache.http.impl.client.HttpClients;
      9 import org.apache.http.util.EntityUtils;
     10 import org.testng.Assert;
     11 import org.testng.annotations.AfterClass;
     12 import org.testng.annotations.BeforeClass;
     13 import org.testng.annotations.DataProvider;
     14 import org.testng.annotations.Test;
     15 
     16 import java.io.IOException;
     17 import java.net.URI;
     18 import java.net.URISyntaxException;
     19 
     20 /**
     21  * Created by zhangbin on 16/4/26.
     22  * 接口链接:http://gateway.zitech.com/gw/oauthentry/study.scene/1.0/getbysid?" 参数access_token,subject_id
     23  * asset过程中出现字符类型不同问题,字符类型必须相同才能进行asset
     24  */
     25 public class HttpGetTest {
     26 
     27 
     28     @BeforeClass
     29     public void beforeClass(){
     30         System.out.println("=======This is BeforeClass=======");
     31     }
     32 
     33     @DataProvider(name="user")
     34     public Object [][] Users(){
     35         return new Object[][]{
     36                 {"bb801ee6b9983c7f893473fd6a313ad2","1","0"},
     37                 {"bb801ee6b9983c7f893473fd6a313ad2","1,2","5008"},
     38                 {"bb801ee6b9983c7f893473fd6a313ad2","123","0"},
     39                 {"bb801ee6b9983c7f893473fd6a313ad2","","5008"}
     40         };
     41 
     42     }
     43 
     44     @Test(dataProvider="user")
     45     public void getUri(String access_token,String subject_id,String hcode) throws URISyntaxException, IOException {
     46         /**
     47          * 拼接接口链接
     48          */
     49         URI uri = new URIBuilder()
     50                 .setScheme("http")
     51                 .setHost("gateway.zitech.com/gw/oauthentry")
     52                 .setPath("/study.scene/1.0/getbysid")
     53                 .setParameter("access_token",access_token)
     54                 .setParameter("subject_id",subject_id)
     55                 .build();
     56         System.out.println(uri);
     57         /**
     58          * 创建httpclient
     59          */
     60         CloseableHttpClient httpClient = HttpClients.createDefault();
     61         HttpGet httpGet = new HttpGet(uri);
     62         /**
     63          * 设置超时时间
     64          */
     65         RequestConfig requestConfig = RequestConfig.custom()
     66                 .setSocketTimeout(2000)
     67                 .setConnectionRequestTimeout(2000)
     68                 .build();
     69         httpGet.setConfig(requestConfig);
     70 
     71         CloseableHttpResponse response = httpClient.execute(httpGet);
     72         System.out.println("响应码:" + response.getStatusLine().getStatusCode());
     73         /**
     74          * 获取服务器响应内容
     75          */
     76         String returnStr = EntityUtils.toString(response.getEntity());
     77 //        System.out.println("响应内容:" + returnStr);
     78 
     79         /**
     80          * 解析json
     81          */
     82         JSONObject jsonObject = JSONObject.fromObject(returnStr);
     83 //        System.out.println(jsonObject.has("message"));
     84 //        System.out.println(jsonObject.getString("message"));
     85         String code = jsonObject.getString("code");
     86         String message = jsonObject.getString("message");
     87         /**
     88          * 进行结果验证
     89          * 注意asset对象字符类型,此处全为String类型
     90          */
     91         if (jsonObject.get("data") !=null && code.equals("0")){
     92 
     93             JSONArray array = jsonObject.getJSONArray("data");
     94             if (array.size() > 0){
     95                 JSONObject data = array.getJSONObject(0);
     96                 String su_id = data.getString("subjectId");
     97                 System.out.println("subjectId:" + su_id);
     98                 Assert.assertEquals(subject_id,su_id);
     99 
    100             }else {
    101                 Assert.assertEquals(hcode,code);
    102                 System.out.println(code);
    103             }
    104         }
    105         else {
    106             Assert.assertEquals(hcode,code);
    107         }
    108         System.out.println("message:" + message);
    109 
    110     }
    111 
    112     @AfterClass
    113     public void afterClass(){
    114         System.out.println("This is afterClass");
    115     }
    116 
    117 
    118 
    119 }
  • 相关阅读:
    点击图片显示原图
    SQL判断语句
    窗口淡入淡出效果
    判断两段时间之间的时间差
    软件行业发展趋势
    VSS客户端不能访问问题“unable to open user login file\\服务器项目管理目录\data\logedin\用户名.log ”
    鑫哥儿子顺利降生了!
    面向对象原则之单一职责原则实现
    PHP编码,乱码问题
    泛型中的default(T)
  • 原文地址:https://www.cnblogs.com/testJocab/p/5438585.html
Copyright © 2011-2022 走看看