一:創(chuàng)建項目,引入包依賴
我創(chuàng)建的是Java的maven項目,在pom.xml下引入testng和httpclient
httpclient:基于http協(xié)議,客戶端 HTTP 協(xié)議傳輸類庫,用戶發(fā)送和接受http的消息
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>6.9.9</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.2</version>
</dependency>
二:封裝post和get方法
創(chuàng)建一個工具類RestClient
//get請求方法,不帶請求頭
public CloseableHttpResponse get(String url) throws ClientProtocolException, IOException {
//創(chuàng)建一個可關(guān)閉的HttpClient對象
CloseableHttpClient httpclient = HttpClients.createDefault();
//創(chuàng)建一個httpget對象
HttpGet httpget = new HttpGet(url);
//執(zhí)行請求,相當于瀏覽器點擊發(fā)送按鈕,然后賦值給HttpResponse對象接收
CloseableHttpResponse httpResponse = httpclient.execute(httpget);
//返回請求的結(jié)果
return httpResponse;
}
//get帶請求頭
public CloseableHttpResponse get(String url,HashMap<String,String> headermap) throws ClientProtocolException, IOException{
CloseableHttpClient httpclient=HttpClients.createDefault();
HttpGet httpget=new HttpGet(url);
for(Map.Entry<String, String> entry:headermap.entrySet()){
httpget.setHeader(entry.getKey(), entry.getValue());
}
CloseableHttpResponse httpResponse=httpclient.execute(httpget);
return httpResponse;
}
//post方法
public CloseableHttpResponse post(String url,String entityString,HashMap<String,String> headermap) throws ClientProtocolException, IOException{
//創(chuàng)建一個httpClient對象
CloseableHttpClient httpclient=HttpClients.createDefault();
//創(chuàng)建一個httppost對象
HttpPost httppost=new HttpPost(url);
HttpEntity entity=new StringEntity(entityString.toLowerCase());
//設(shè)置一個payload
httppost.setEntity(entity);
//加載請求頭到httppost對象
if(headermap!=null){
for(Map.Entry<String, String> entry : headermap.entrySet()){
httppost.addHeader(entry.getKey(), entry.getValue());
}
}
//發(fā)送請求
CloseableHttpResponse httpResponse = httpclient.execute(httppost);
//返回請求的結(jié)果
return httpResponse;
}
三:測試接口
創(chuàng)建一個test執(zhí)行的類,使用@Test標記
public class DoubanTest {
CloseableHttpResponse closeableHttpResponse;
@Test
public void getMovie() throws IOException {
//接口地址
String url="這是要測試的api";
RestClient rest=new RestClient();
//構(gòu)造請求頭信息
HashMap<String,String> headermap = new HashMap<String,String>();
headermap.put("Content-Type", "application/json");
//構(gòu)造請求的參數(shù)信息
JsonObject json=new JsonObject();
json.addProperty("page", "1");
json.addProperty("count", "2");
json.addProperty("type","video");
//發(fā)送post請求
closeableHttpResponse=rest.post(url,json.toString(),headermap);
//獲取接口返回狀態(tài)碼
int statusCode=closeableHttpResponse.getStatusLine().getStatusCode();
System.out.println("the code is"+statusCode);
//獲取接口的返回內(nèi)容
String responseString = EntityUtils.toString(closeableHttpResponse.getEntity());
System.out.println("the response is:"+responseString);
}
}
運行test類,在控制臺打印出接口相關(guān)信息

接口返回打印信息.png
這樣最基礎(chǔ)的一個接口測試框架算是跑通了