Java实现接口测试的实战练习
1.基本的结构
2.代码实现如下
Common.java
package findyou.Interface;
import org.codehaus.jettison.json.JSONException;//jason解析的方法
import org.codehaus.jettison.json.JSONObject;//jason解析的方法
public class Common {
/**
* 解析Json内容
*
* @author Findyou
* @version 1.0 2015/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);// 以 Java 编程语言中 String 的形式获取此 ResultSet 对象的当前行中指定列的值
} catch (JSONException e) {
e.printStackTrace();在命令行打印异常信息在程序中出错的位置及原因。
}
return JsonValue;
}
}
getCityWeather.java
package findyou.Interface;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
public class getCityWeather {
private 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 {
HttpURLConnection connection = URLConnection.getConnection(url);
DataOutputStream out = null;
// 建立实际的连接
connection.connect();
out = new DataOutputStream(connection.getOutputStream());
out.flush();
out.close();
BufferedReader reader = new BufferedReader(new InputStreamReader(
connection.getInputStream()));
while ((line = reader.readLine()) != null) {
httpResults = httpResults + line.toString();
}
reader.close();
// 断开连接
connection.disconnect();
} catch (Exception e) {
e.printStackTrace();
}
return httpResults;
}
}
URLConnection.java
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(true);//设置是否向httpUrlConnection输出
connection.setDoInput(true);//设置是否从httpUrlConnection读入,默认情况下是true
connection.setRequestMethod("GET");//设定请求的方法为"get"
connection.setUseCaches(false);//是否可以使用缓存,Post 请求不能使用缓存
connection.setInstanceFollowRedirects(true);//是否使用重定向
connection.setRequestProperty("Content-Type", "application/json");//是否使用重定向
connection.setRequestProperty("Charset", "utf-8");//(如果不设此项,在传送序列化对象时,当WEB服务默认的不是这种类型时可能抛java.io.EOFException)
connection.setRequestProperty("Accept-Charset", "utf-8");//
} catch (Exception e) {
e.printStackTrace();
}
return connection;
}
}
TestCase
RequestTest .java
package findyou.testcase;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStreamWriter;
import java.net.URL;
import java.net.URLConnection;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.commons.io.IOUtils;
public class RequestTest {
public static void main(String[] args){
try{
// Configure and open a connection to the site you will send the request
URL url = new URL("http://www.iana.org/domains/example/");
URLConnection urlConnection = url.openConnection();
// 设置doOutput属性为true表示将使用此urlConnection写入数据
urlConnection.setDoOutput(true);
// 定义待写入数据的内容类型,我们设置为application/x-www-form-urlencoded类型
urlConnection.setRequestProperty("content-type", "application/x-www-form-urlencoded");
// 得到请求的输出流对象
OutputStreamWriter out = new OutputStreamWriter(urlConnection.getOutputStream());
//OutputStreamWriter是Writer的子类,将输出的字符流变为字节流,即:将一个字符流的输出对象变为字节流的输出对象。
//getOutputStream返回Servlet引擎创建的字节输出流对象
// 把数据写入请求的Body
out.write("message = Hello World chszs");
out.flush();
out.close();
// 从服务器读取响应
InputStream inputStream = urlConnection.getInputStream();// 得到
String encoding = urlConnection.getContentEncoding();
String body = IOUtils.toString(inputStream, encoding);
System.out.println(body);
}catch(IOException e){
Logger.getLogger(RequestTest.class.getName()).log(Level.SEVERE, null, e);
}
}
}
test.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";
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);//拼接url并且获得请求的URL
Reporter.log("请求地址: "+weather.geturl());//输出地址
Reporter.log("返回结果: "+httpResult);
weatherinfo=Common.getJsonValue(httpResult, "weatherinfo");//将respose的内容转换成jason格式,然后截取部分内容(weatherinfo)
city=Common.getJsonValue(weatherinfo, "city");//将respose的内容转换成jason格式,然后截取部分内容(weatherinfo)
Reporter.log("用例结果: resultCode=>expected: " + exp_city_str + " ,actual: "+ city);//输出
Assert.assertEquals(city,exp_city_str); //实际截取的内容和输入的内容是否一致
}
}
Java实现接口测试的实战练习相关推荐
- python接口测试框架实战与自动化进阶(三)
python接口测试框架实战与自动化进阶 一.持续集成 1.持续集成环境搭建 1)安装Jenkins 官网下载后直接安装:https://jenkins.io/ 终端直接安装及启动:java -jar ...
- Scala程序设计:Java虚拟机多核编程实战(国内第一本Scala图书)
Scala程序设计:Java虚拟机多核编程实战(国内第一本Scala图书) 基本信息 作者: (美)Venkat Subramaniam 译者: 郑晔 李剑 丛书名: 图灵程序设计丛书 出版社:人民 ...
- java ee核心框架实战 pdf_Java EE核心框架实战 高洪岩 中文PDF
资源名称:Java EE核心框架实战 高洪岩 中文PDF 第1章 MyBatis 3操作数据库 第2章 MyBatis 3常用技能 第3章 Struts 2必备开发技能 第4章 Struts 2文件的 ...
- 【直播回顾】云栖社区特邀专家徐雷Java Spring Boot开发实战系列课程(第19讲):Java Spring Cloud微服务架构模式与开发实战...
主讲人:徐雷(云栖社区特邀Java专家) 徐雷,花名:徐雷frank:资深架构师,MongoDB中文社区联席主席,吉林大学计算机学士,上海交通大学硕士.从事了 10年+开发工作,专注于分布式架构,Ja ...
- Java 7并发编程实战手册
2019独角兽企业重金招聘Python工程师标准>>> Java 7并发编程实战手册 本书是 Java 7 并发编程的实战指南,介绍了Java 7 并发API 中大部分重要而有用的机 ...
- 《Java EE核心框架实战》—— 2.3 resultMap 标签
本节书摘来异步社区<Java EE核心框架实战>一书中的第2章,第2.3节,作者: 高洪岩,更多章节内容可以访问云栖社区"异步社区"公众号查看. 2.3 < re ...
- Java RESTful Web Service实战
编者按: InfoQ开设栏目"品味书香",精选技术书籍的精彩章节,以及分享看完书留下的思考和收获,欢迎大家关注.本文节选自韩陆著<Java RESTful Web Servi ...
- 《Java和Android开发实战详解》——2.5节良好的Java程序代码编写风格
本节书摘来自异步社区<Java和Android开发实战详解>一书中的第2章,第2.5节良好的Java程序代码编写风格,作者 陈会安,更多章节内容可以访问云栖社区"异步社区&quo ...
- Java 高并发_JAVA并发编程与高并发解决方案 JAVA高并发项目实战课程 没有项目经验的朋友不要错过!...
JAVA并发编程与高并发解决方案 JAVA高并发项目实战课程 没有项目经验的朋友不要错过! 1.JPG (37.82 KB, 下载次数: 0) 2018-12-3 09:40 上传 2.JPG (28 ...
最新文章
- 赛门铁克开启“容灾即服务”时代
- Hibernate 注解学习
- 001-ant design安装及快速入门【基于纯antd的基本项目搭建】
- 2021天津高考成绩查询时间26号几点,2021年天津高考成绩查询时间安排 什么时候出分...
- Spring MVC 使用优化建议-缓存URL
- 断电,软件崩溃,系统中毒,未点击保存,就关闭导致资料丢失,以word文件文件为例,如何找回
- Android基于Glide的二次封装,借鉴Glide思想二次封装Fresco
- 【转】css_重绘重排
- 蓝桥杯 BASIC-18 基础练习 矩形面积交
- Python_机器学习_常用科学计算库_第5章_Pandas
- js - JSON构造函数
- 地理社交网络中基于多目标组合优化的空间感知影响力联合最大化
- 解决Windows 10环境下 Realtek声卡 台式机前面板插孔没有声音输出问题
- 采用java编写一个软件,100以内的口算题【软件构造大作业】
- 《管理者必读12篇》:如何做好人员管理?
- python自动运行
- 假期即将结束,前方道路还很长……CLANNAD,16话,渚就这么香消玉殒了……
- mysql 备份 发送邮件_mysql 自动备份发邮件 到指定邮箱
- SpringBoot启动报错 Failed to read candidate component class:
- JAVA Set 交集,差集,并集