• Java的get请求-----接口测试


    package findyou.Interface;
    
    import java.net.HttpURLConnection;
    import java.net.URL;
    
    public class URLConnection {	
    	public static HttpURLConnection getConnection(String url){
    		HttpURLConnection connection = null;
    		try {
    			// 打开和URL之间的连接
    			URL postUrl = new URL(url);
    			connection = (HttpURLConnection) postUrl.openConnection();
    			 // 设置通用的请求属性
    			connection.setDoOutput(false);//posp请求改为TRUE
    			connection.setDoInput(true);
    			connection.setRequestMethod("GET");
    			connection.setUseCaches(false);
    			connection.setInstanceFollowRedirects(true);
    			connection.setRequestProperty("Content-Type", "application/json");
    			connection.setRequestProperty("Charset", "utf-8");
    			connection.setRequestProperty("Accept-Charset", "utf-8");
    		} catch (Exception e) {
    			e.printStackTrace();
    		} 
    		return connection;
    	}
    }
    

      

    package findyou.Interface;
    
    import java.io.BufferedReader;
    import java.io.DataOutputStream;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.net.HttpURLConnection;
    import java.net.MalformedURLException;
    public class getCityWeather {
    
         private static String url="";
    
         
    
         public String geturl() {
    
             return url;
    
         }
    
    
    
      public String getHttpRespone(String cityCode) throws IOException {
    
             String line = "";
    
             String httpResults = "";
    
             url=("http://www.weather.com.cn/data/cityinfo/"
    
                     + cityCode + ".html");
    
             try {
     			//url=("http://www.weather.com.cn/data/cityinfo/"+ cityCode + ".html");
     			HttpURLConnection urlcon = URLConnection.getConnection(url);      
     			urlcon.setReadTimeout(5000);
     			urlcon.setConnectTimeout(5000);
     			urlcon.connect();
     			BufferedReader reader=new BufferedReader(new InputStreamReader(urlcon.getInputStream(),"utf-8"));
     			 //line=bf.readLine();
     			 while ((line = reader.readLine()) != null) {
                     httpResults = httpResults + line.toString();
                 }
     			
     		} catch (MalformedURLException e) {
     			// TODO Auto-generated catch block
     			e.printStackTrace();
     		}
             return httpResults;
    
         }
    
        
    }
    

      

    package findyou.Interface;
    
    import org.codehaus.jettison.json.JSONException;
    import org.codehaus.jettison.json.JSONObject;
    
    public class Common {
    	/**
    	 * 解析Json内容
    	 * 
    	 * @author Findyou
    	 * @version 1.0 2016/3/23
    	 * @return JsonValue 返回JsonString中JsonId对应的Value
    	 **/
    	public static String getJsonValue(String JsonString, String JsonId) {
    		String JsonValue = "";
    		if (JsonString == null || JsonString.trim().length() < 1) {
    			return null;
    		}
    		try {
    			JSONObject obj1 = new JSONObject(JsonString);
    			JsonValue = (String) obj1.getString(JsonId);
    		} catch (JSONException e) {
    			e.printStackTrace();
    		}
    		return JsonValue;
    	}
    }
    

      下面的代码是用Java写的测试用例

    package findyou.testcase;
    
    import java.io.IOException;
    
    import org.testng.Assert;
    import org.testng.Reporter;
    import org.testng.annotations.Test;
    
    import findyou.Interface.Common;
    import findyou.Interface.getCityWeather;
    
    public class test {
    	public String httpResult= null, weatherinfo= null, city=null,exp_city = null;
    	public static String cityCode="";	
    	getCityWeather weather=new getCityWeather();
    	
    	@Test(groups = { "BaseCase"})
    	public void getShenZhen_Succ() throws IOException{
    		exp_city="深圳";
    		cityCode="101280601";
    		Reporter.log("【正常用例】:获取"+exp_city+"天气成功!");
    		httpResult=weather.getHttpRespone(cityCode);
    		Reporter.log("请求地址: "+weather.geturl());
    		Reporter.log("返回结果: "+httpResult);
    		weatherinfo=Common.getJsonValue(httpResult, "weatherinfo");
    		city=Common.getJsonValue(weatherinfo, "city");
    		Reporter.log("用例结果: resultCode=>expected: " + exp_city + " ,actual: "+ city);
    		Assert.assertEquals(city,exp_city);
    	}
    	
    	@Test(groups = { "BaseCase"})
    	public void getBeiJing_Succ() throws IOException{
    		exp_city="北京";
    		cityCode="101010100";
    		Reporter.log("【正常用例】:获取"+exp_city+"天气成功!");
    		httpResult=weather.getHttpRespone(cityCode);
    		Reporter.log("请求地址: "+weather.geturl());
    		Reporter.log("返回结果: "+httpResult);
    		weatherinfo=Common.getJsonValue(httpResult, "weatherinfo");
    		city=Common.getJsonValue(weatherinfo, "city");
    		Reporter.log("用例结果: resultCode=>expected: " + exp_city + " ,actual: "+ city);
    		Assert.assertEquals(city,exp_city);
    	}
    	
    	@Test(groups = { "BaseCase"})
    	public void getShangHai_Succ() throws IOException{
    		exp_city="上海";
    		cityCode="101020100";
    		Reporter.log("【正常用例】:获取"+exp_city+"天气成功!");
    		httpResult=weather.getHttpRespone(cityCode);
    		Reporter.log("请求地址: "+weather.geturl());
    		Reporter.log("返回结果: "+httpResult);
    		weatherinfo=Common.getJsonValue(httpResult, "weatherinfo");
    		city=Common.getJsonValue(weatherinfo, "city");
    		Reporter.log("用例结果: resultCode=>expected: " + exp_city + " ,actual: "+ city);
    		Assert.assertEquals(city,exp_city);
    	}	
    }
    

      

    package findyou.testcase;
    
    import java.io.IOException;
    
    import org.testng.Assert;
    import org.testng.Reporter;
    import org.testng.annotations.Test;
    
    import findyou.Interface.Common;
    import findyou.Interface.getCityWeather;
    
    public class test1 {
    	public String httpResult= null, weatherinfo= null, city=null,exp_city = null;
    	public static String cityCode="";
    	getCityWeather weather=new getCityWeather();
    	
    	
    	@Test(groups = { "BaseCase"})
    	public void getShenZhen_Succ() throws IOException{
    		exp_city="深圳";
    		cityCode="101280601";
    		resultCheck(cityCode, exp_city);
    	}
    	
    	@Test(groups = { "BaseCase"})
    	public void getBeiJing_Succ() throws IOException{
    		exp_city="北京";
    		cityCode="101010100";
    		resultCheck(cityCode, exp_city);
    	}
    	
    	@Test(groups = { "BaseCase"})
    	public void getShangHai_Succ() throws IOException{
    		exp_city="上海";
    		cityCode="101020100";
    		resultCheck(cityCode, exp_city);
    	}
    	
    	public void resultCheck(String cityCode_str, String exp_city_str) throws IOException{
    		Reporter.log("【正常用例】:获取"+exp_city_str+"天气成功!");
    		httpResult=weather.getHttpRespone(cityCode_str);
    		Reporter.log("请求地址: "+weather.geturl());
    		Reporter.log("返回结果: "+httpResult);
    		weatherinfo=Common.getJsonValue(httpResult, "weatherinfo");
    		city=Common.getJsonValue(weatherinfo, "city");
    		Reporter.log("用例结果: City=>expected: " + exp_city_str + " ,actual: "+ city);
    		Assert.assertEquals(city,exp_city_str);		
    	}
    }
    

      

  • 相关阅读:
    字节编码中文编码方式总结
    参数读取一个关于java.net.Socket的超时的问题
    属性处理器Spring攻略学习笔记(2.12)外部化Bean配置
    设置编译器让CodeBlock支持C99标准
    相机标记[置顶] OpenCV for Ios 学习笔记(6)-标记检测3
    宋体配置JAVA j2ee (一) 轻松入门
    连接固件Jlink v8固件修复
    css的使用和基本语法
    css语言基础css的选择符语法
    css语言基础css属性值的规范
  • 原文地址:https://www.cnblogs.com/wangxiaoqun/p/6721529.html
Copyright © 2020-2023  润新知