testNG+httpclientインターフェーステスト(eclipse+idea)

4190 ワード

1.環境、プラットフォーム
 1.1 JDK 1.7+eclipse
まずeclipseにtestNGプラグインをインストールして、インストールの過程http://www.blogjava.net/qileilove/archive/2014/09/02/417593.html
 
httpclient 4.3 jarのカバンをネットでダウンロードして、住所をダウンロードします。 http://download.csdn.net/detail/u010627840/8192601
JSON関連jarパッケージをダウンロードして、住所をダウンロードします。 https://mvnrepository.com/artifact/com.alibaba/fastjson/1.2.62
testNG+httpclient接口测试(eclipse+idea)_第1张图片
1.2 maven+idea+testNg 
依存のjar

  org.testng
  testng
  6.8


  org.apache.httpcomponents
  httpclient
  4.3




  com.alibaba
  fastjson
  1.2.62
  testNG+httpclient接口测试(eclipse+idea)_第2张图片
 
2目的
 シミュレーションブラウザがget要求を行い、戻ったJSONを取得し、解析します。
 
 
package com.example.shard.test;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.apache.http.HttpEntity;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;


public class HttpGetTest {

    @BeforeClass
    public void beforeClass() {
        System.out.println("==========this is before class===============");
    }

    @Test
    public void testGet() throws Exception {
        requestGet("http://119.147.19.43/v3/user/get_info");
//    	requestGet("http://www.renren.com/PLogin.do");
    }

    private void requestGet(String urlWithParams) throws Exception {
        CloseableHttpClient httpclient = HttpClientBuilder.create().build();

        HttpGet httpget = new HttpGet(urlWithParams);
        //         
        RequestConfig requestConfig = RequestConfig.custom()
                .setConnectionRequestTimeout(50)
                .setConnectTimeout(50)
                .setSocketTimeout(50).build();
        httpget.setConfig(requestConfig);

        CloseableHttpResponse response = httpclient.execute(httpget);
        System.out.println("   StatusCode -> " + response.getStatusLine().getStatusCode());

        HttpEntity entity = response.getEntity();
        String jsonStr = EntityUtils.toString(entity);//, "utf-8");

        char c = jsonStr.trim().charAt(0);
        if ('[' == c) {
            JSONArray jsonArray = JSONArray.parseArray(jsonStr);
            for (int i = 0; i < jsonArray.size(); i++) {
                JSONObject jsonObj = jsonArray.getJSONObject(i);
                System.out.println("jsonObj=:" + jsonObj);
            }
        } else if ('{' == c) {
            JSONObject jsonObj1 = JSONObject.parseObject(jsonStr);
            System.out.println("      :jsonObj1=" + jsonObj1);


        }

        httpget.releaseConnection();
    }

    @AfterClass
    public void afterClass() {
        System.out.println("===========this is after class=================");
    }
}