1. 程式人生 > >java介面自動化4——PUT和Delete請求方法封裝和測試

java介面自動化4——PUT和Delete請求方法封裝和測試

接著上面一篇,這篇來封裝下PUT和Delete方法。雖然這兩個方法很少用,這篇內容就算了解一下。PUT方法封裝完成參考POST方法,Delete方法封裝可以參考GET方法。由於弄明白了前面的Get和Post方法封裝過程,現在就直接貼出封裝方法和測試程式碼。

1.PUT和Deletet方法

package com.qa.restclient;
 
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
 
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpDelete;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
 
public class RestClient {
	
	
	//1. Get 請求方法
	public CloseableHttpResponse get(String url) throws ClientProtocolException, IOException {
		
		//建立一個可關閉的HttpClient物件
		CloseableHttpClient httpclient = HttpClients.createDefault();
		//建立一個HttpGet的請求物件
		HttpGet httpget = new HttpGet(url);
		//執行請求,相當於postman上點擊發送按鈕,然後賦值給HttpResponse物件接收
		CloseableHttpResponse httpResponse = httpclient.execute(httpget);
		
		return httpResponse;
	}
	
	//2. Get 請求方法(帶請求頭資訊)
	public CloseableHttpResponse get(String url,HashMap<String,String> headermap) throws ClientProtocolException, IOException {
			
		//建立一個可關閉的HttpClient物件
		CloseableHttpClient httpclient = HttpClients.createDefault();
		//建立一個HttpGet的請求物件
		HttpGet httpget = new HttpGet(url);
		//載入請求頭到httpget物件
		for(Map.Entry<String, String> entry : headermap.entrySet()) {
			httpget.addHeader(entry.getKey(), entry.getValue());
		}
		//執行請求,相當於postman上點擊發送按鈕,然後賦值給HttpResponse物件接收
		CloseableHttpResponse httpResponse = httpclient.execute(httpget);
			
		return httpResponse;
	}
	
	//3. POST方法
	public CloseableHttpResponse post(String url, String entityString, HashMap<String,String> headermap) throws ClientProtocolException, IOException {
		//建立一個可關閉的HttpClient物件
		CloseableHttpClient httpclient = HttpClients.createDefault();
		//建立一個HttpPost的請求物件
		HttpPost httppost = new HttpPost(url);
		//設定payload
		httppost.setEntity(new StringEntity(entityString));
		
		//載入請求頭到httppost物件
		for(Map.Entry<String, String> entry : headermap.entrySet()) {
			httppost.addHeader(entry.getKey(), entry.getValue());
		}
		//傳送post請求
		CloseableHttpResponse httpResponse = httpclient.execute(httppost);
		return httpResponse;
	}
	
	//4. Put方法
	public CloseableHttpResponse put(String url, String entityString, HashMap<String,String> headerMap) throws ClientProtocolException, IOException {
		
		CloseableHttpClient httpclient = HttpClients.createDefault();
		HttpPut httpput = new HttpPut(url);
		httpput.setEntity(new StringEntity(entityString));
	
		for(Map.Entry<String, String> entry : headerMap.entrySet()) {
			httpput.addHeader(entry.getKey(), entry.getValue());
		}
		//傳送put請求
		CloseableHttpResponse httpResponse = httpclient.execute(httpput);
		return httpResponse;
	}
	
	//5. Delete方法
	public CloseableHttpResponse delete(String url) throws ClientProtocolException, IOException {
			
		CloseableHttpClient httpclient = HttpClients.createDefault();
		HttpDelete httpdel = new HttpDelete(url);
		
		//傳送put請求
		CloseableHttpResponse httpResponse = httpclient.execute(httpdel);
		return httpResponse;
	}
}

2.測試程式碼

1)Put方法測試

package com.qa.tests;
 
import java.io.IOException;
import java.util.HashMap;
 
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.util.EntityUtils;
import org.testng.Assert;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
 
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.qa.base.TestBase;
import com.qa.data.Users;
import com.qa.restclient.RestClient;
import com.qa.util.TestUtil;
 
public class PutApiTest extends TestBase {
	
	TestBase testBase;
	String host;
	String url;
	RestClient restClient;
	CloseableHttpResponse closeableHttpResponse;
	
	
	@BeforeClass
	public void setUp() {
		testBase = new TestBase();
		host = prop.getProperty("HOST");
		url = host + "/api/users/2";
		
	}
	
	@Test
	public void putTest() throws ClientProtocolException, IOException{
		restClient = new RestClient();
		HashMap<String,String> headermap = new HashMap<String,String>();
		headermap.put("Content-Type", "application/json"); //這個在postman中可以查詢到
		
		//物件轉換成Json字串
		Users user = new Users("Anthony","automation tester");
		String userJsonString = JSON.toJSONString(user);
		//System.out.println(userJsonString);
		
		closeableHttpResponse = restClient.put(url, userJsonString, headermap);
		
		//驗證狀態碼是不是200
		int statusCode = closeableHttpResponse.getStatusLine().getStatusCode();
		Assert.assertEquals(statusCode, RESPNSE_STATUS_CODE_200,"response status code is not 200");
		
		//驗證名稱為Anthony的jon是不是 automation tester
		String responseString = EntityUtils.toString(closeableHttpResponse.getEntity());
		JSONObject responseJson = JSON.parseObject(responseString);
		String jobString = TestUtil.getValueByJPath(responseJson, "job");
		System.out.println(jobString);
		Assert.assertEquals(jobString, "automation tester","job is not same");
	}
 
}

2) Delete方法測試

package com.qa.tests;
 
import java.io.IOException;
 
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.testng.Assert;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
 
import com.qa.base.TestBase;
import com.qa.restclient.RestClient;
 
public class DeleteApiTest extends TestBase {
	TestBase testBase;
	String host;
	String url;
	RestClient restClient;
	CloseableHttpResponse closeableHttpResponse;
	
	
	@BeforeClass
	public void setUp() {
		testBase = new TestBase();
		host = prop.getProperty("HOST");
		url = host + "/api/users/2";  //直接在這個網站可以找到delete的api
		
	}
	
	@Test
	public void deleteApiTest() throws ClientProtocolException, IOException {
		restClient = new RestClient();
		closeableHttpResponse = restClient.delete(url);
		
		int statusCode = closeableHttpResponse.getStatusLine().getStatusCode();
		System.out.println(statusCode);
		Assert.assertEquals(statusCode, 204,"status code is not 204");
	}
}

注意到我們寫過的TestNG測試用例沒,我們發現每次寫響應碼斷言和json內容判斷,我們都需要多次重複寫這些程式碼。我們既然看到了重複的程式碼,就要想辦法去消除,抽取出來成公共方法來呼叫。下篇,我們實現這個優化和實現日誌列印功能。

轉載博文:https://blog.csdn.net/u011541946/article/details/80434593