SpringBoot是Java生态下的一个快速Web实现的微框架,最近使用它来实现对子系统接口的测试,这个系统仅仅是系统的一部分作为控制台来使用,其他还有两个系统,一个是从控制台获取Json格式的信息,处理后生成对外接口有两个,一个是提供给仓库,一个提供给SDK系统。对每一个接口的功能测试是从浏览器中访问 URL 地址来实现。

  1. 使用 Java 语言;
  2. 使用 JdbcTemplate 库访问Mysql;
  3. 使用 HttpClient
  4. 既有GET方法也有POST方法访问系统;POST访问前的数据封装需要注意,我这里使用 HttpEntity entity = new StringEntity(….)
    package me.code.controller;import com.alibaba.fastjson.JSON;import org.apache.http.HttpEntity;import org.apache.http.NameValuePair;import org.apache.http.client.entity.UrlEncodedFormEntity;import org.apache.http.client.methods.CloseableHttpResponse;import org.apache.http.client.methods.HttpGet;import org.apache.http.client.methods.HttpPost;import org.apache.http.entity.StringEntity;import org.apache.http.impl.client.CloseableHttpClient;import org.apache.http.impl.client.HttpClients;import org.apache.http.message.BasicNameValuePair;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.jdbc.core.JdbcTemplate;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;import shiver.me.timbers.data.random.RandomStrings;import java.io.IOException;import java.io.InputStream;import java.util.ArrayList;import java.util.List;/*** Created by wfan on 16/4/29.*/@RestController@RequestMapping("/add")public class TestCustomAppInterface {@AutowiredJdbcTemplate jdbcTemplate;RandomStrings randomStrings = new RandomStrings();String appName = randomStrings.someAlphaString(8);@RequestMapping("/app")public String addapp() throws IOException {//clean the table content firstSystem.out.println("start to delete app table");String table = "victoria_custom_app";this.cleanDBTableContent(table);int num = getCountFromTable(table);if (0 == num)System.out.println("clean app table successfully!");elseSystem.out.println("failed to clean app table !");CloseableHttpClient httpClient = HttpClients.createDefault();String factorIds = "1,20,21,22,23,24,20,1,21,22,23,24,";String uri = "http://localhost:9090/addCustomApp?appid=" + appName + "&factorIds=" + factorIds;HttpGet httpGet = new HttpGet(uri);CloseableHttpResponse response = httpClient.execute(httpGet);String getHeader = null;response.getHeaders(getHeader);System.out.println("Header info is : " + getHeader);//return getHeader;InputStream inputStream = null;inputStream = response.getEntity().getContent();boolean isEmpty = this.getInfoByAppName(table, appName, factorIds);int status = response.getStatusLine().getStatusCode();if (isEmpty)System.out.println("ERROR : FAIL TO ADD CUSTOM APP NAME ! ");else if (!isEmpty && 200==status)System.out.println("ADD NEW APP NAME SUCCESSFULLY ! ");//TODO FOR LOGreturn "response content is " + inputStream.toString() + "\n" + "  status is  "+ response.getStatusLine().getStatusCode()+ "  appname is " + appName+ " number is after call clean function " + num;}@RequestMapping("/rule")public String addRule() throws IOException{cleanDBTableContent("victoria_custom_rule");int ruleCount = getCountFromTable("victoria_custom_rule");if (0 == ruleCount)System.out.println("clean rule table successfully!");elseSystem.out.println("failed to clean rule table !");CloseableHttpClient httpClient = HttpClients.createDefault();String curAppName = appName;//String ruleValue = "[{'description':'undefined', 'subsidy_score':'0.06', 'negative_complaints_score':'-0.02'}]";String uri = "http://localhost:9090/saveCustomRules?app=" + curAppName;//+ "&object=" + ruleValue;HttpPost httpPost = new HttpPost(uri);//List<NameValuePair> nvps = new ArrayList<NameValuePair>();//        nvps.add(new BasicNameValuePair("\"description", "undefined1"));//        nvps.add(new BasicNameValuePair("subsidy_score", "0.06"));//        nvps.add(new BasicNameValuePair("nagative_complaints_score", "-0.02"));HttpEntity entity= new StringEntity("[{\"tea_exposure_score\":\"0\",\"negative_reminder_score\":\"-0.01\",\"exposure_score\":\"0\",\"negative_bad_rating_score\":\"-0.02\",\"dinner_exposure_score\":\"0\",\"rid\":1,\"description\":\"undefined\"}]","utf-8");httpPost.setEntity(entity);CloseableHttpResponse response = httpClient.execute(httpPost);return "use app name is " + curAppName+ "response content is " + response.getEntity().getContent()+ " response status is " + response.getStatusLine().getStatusCode();}@RequestMapping("/case")public String saveCases() throws Exception{String testName;String testDate = "2016-05-04%2000:00%20-%202016-05-04%2023:59";int status[] = string2ASCII("编辑中");//delete a record from case table/*String delstr = "delete from victoria_test_case where object=\"芜湖\"";jdbcTemplate.execute(delstr);*/RandomStrings randomStrings = new RandomStrings();testName = randomStrings.someAlphaString(6);CloseableHttpClient httpClient = HttpClients.createDefault();String uri = "http://localhost:9090/saveCases?testName=" + testName + "&testDate=" + testDate + "&status=" + status;HttpPost httpPost = new HttpPost(uri);HttpEntity entity = new StringEntity("[{\"description\":\"234\",\"id\":1,\"object\":\"芜湖\",\"rule_id\":\"1\",\"sample_weight\":\"12%\"}]","utf-8");httpPost.setEntity(entity);CloseableHttpResponse response = httpClient.execute(httpPost);return "response status is " + response.getStatusLine().getStatusCode();}public void cleanDBTableContent(String tableName) throws IOException{String sql = "delete from " + tableName;jdbcTemplate.execute(sql);}public int getCountFromTable(String tableName){//String sql = "select count(*) from " + tableName;String sql = "select * from " + tableName;List re = jdbcTemplate.queryForList(sql);return re.size();}public boolean getInfoByAppName(String tableName, String appName, String factors){String sql = "select * from " + tableName + " where app_name = " + "\"" + appName + "\" " +"and factor_ids = " + "\"" + factors +"\"";List re = jdbcTemplate.queryForList(sql);return re.isEmpty();}public static String gbEncoding(final String gbString) {char[] utfBytes = gbString.toCharArray();String unicodeBytes = "";for (int byteIndex = 0; byteIndex < utfBytes.length; byteIndex++) {String hexB = Integer.toHexString(utfBytes[byteIndex]);if (hexB.length() <= 2) {hexB = "00" + hexB;}unicodeBytes = unicodeBytes + "\\u" + hexB;}System.out.println("unicodeBytes is: " + unicodeBytes);return unicodeBytes;}public static String decodeUnicode(final String dataStr) {int start = 0;int end = 0;final StringBuffer buffer = new StringBuffer();while (start > -1) {end = dataStr.indexOf("\\u", start + 2);String charStr = "";if (end == -1) {charStr = dataStr.substring(start + 2, dataStr.length());} else {charStr = dataStr.substring(start + 2, end);}char letter = (char) Integer.parseInt(charStr, 16); // 16进制parse整形字符串。buffer.append(new Character(letter).toString());start = end;}return buffer.toString();}public static int[] string2ASCII(String s) {// 字符串转换为ASCII码if (s == null || "".equals(s)) {return null;}char[] chars = s.toCharArray();int[] asciiArray = new int[chars.length];for (int i = 0; i < chars.length; i++) {asciiArray[i] = char2ASCII(chars[i]);}return asciiArray;}public static int char2ASCII(char c) {return (int) c;}}

使用SpringBoot快速实现接口测试相关推荐

  1. SpringBoot 快速集成 JWT 实现用户登录认证

    前言:当今前后端分离时代,基于Token的会话保持机制比传统的Session/Cookie机制更加方便,下面我会介绍SpringBoot快速集成JWT库java-jwt以完成用户登录认证. 一.JWT ...

  2. SpringBoot 快速开启事务(附常见坑点)

    SpringBoot 快速开启事务(附常见坑点) 序言:此前,我们主要通过XML配置Spring来托管事务.在SpringBoot则非常简单,只需在业务层添加事务注解(@Transactional ) ...

  3. SpringBoot快速构建项目

    我们再来看一下SpringBoot的快速构建项目,我们都是在集成的IDEA当中,创建一个maven project,在maven project的pom文件里呢,我们再去加SpringBoot相关的坐 ...

  4. Jeecg-Boot 一款基于SpringBoot 快速开发平台

    Jeecg-Boot 一款基于SpringBoot 快速开发平台 Jeecg-Boot 是一款基于SpringBoot+代码生成器的快速开发平台!采用前后端分离架构:SpringBoot,Mybati ...

  5. postman数据保存在哪里_快速掌握接口测试利器Postman

    快速掌握接口测试利器Postman 之前简单写过一篇Postman的使用,这次完善了一些知识点,希望对大家有帮助. Postman简介 Postman是谷歌开发的一款网页调试和接口测试工具,能够发送任 ...

  6. SpringBoot:快速使用Spring

    SpringBoot:快速使用Spirng 约定大于配置:很多配置springboot已经预设好了(和程序员达成约定); 特点:自动配置和起步依赖:依赖传递 spring解决企业级应用开发复杂性而创建 ...

  7. SpringBoot | SpringBoot快速入门

    文章目录 SpringBoot快速入门(一) 1.什么是SpringBoot 2.什么是微服务架构 3.第一个SpringBoot程序 4.SpringBoot自动装配原理 (1)Pom.xml (2 ...

  8. SpringSecurity Oauth2 认证授权(二)springboot快速入门与底层介绍

    集成SpringBoot 快速上手 创建maven工程 导入pom <?xml version="1.0" encoding="UTF-8"?> & ...

  9. SpringBoot快速入门one

    Spring Boot SpringBoot 1.建立第一个SpringBoot项目 模型快速构建 maven构建 2.配置文件 3.配置文件读取和绑定 1.@value 2.Environment ...

最新文章

  1. win messenger启动随outlook explorer
  2. 智能车竞赛技术报告 | 全向行进组 - 沈阳工业大学 - 找不到北队
  3. 18.12.09-C语言练习:兔子繁衍问题 / Fibonacci 数列
  4. mysql解压版下载安装教程_mysql 解压版安装配置方法教程
  5. Redmi K20 Pro尊享版官宣:升级为骁龙855 Plus旗舰平台
  6. wpf Content数据绑定StringFormat起作用的原理和解决(转)
  7. PS案例教程:photoshop路径运算和路径组合操作
  8. Win10双击任务栏的QQ图标就会自动隐藏的解决方案
  9. 快递柜管理系统微信小程序-计算机毕业设计
  10. Redhat下小企鹅输入法的安装
  11. 潘悟云方言计算机,山东方言精组与见晓组声母的分合研究
  12. 杰理之连接杰理蓝牙测试盒标志和后台切去蓝牙的使用【篇】
  13. 将文件复制到FTP服务器时发生错误。 的解决办法
  14. 微型计算机的储存体系如何,存储体系结构
  15. 福利丨0元得3000+视频课精品专栏,邀好友得奖金!
  16. GMIC2013新风向:人人网与凡客强强联合力推优惠专区
  17. 工作缺点和不足及措施_工作中的缺点有哪些?
  18. 通过简单的例子来认识和实践牛顿法
  19. 基于javaweb+jsp的校友信息管理系统
  20. 免费接口API:二维码图片地址 返回识别出的内容。通用文字识别

热门文章

  1. 2021年博士研究生招生大概率延期举行
  2. 昨夜MSN上偶遇伊人
  3. OA办公系统在企业中的主要作用
  4. 云服务器测速脚本_服务器网速测试脚本
  5. 个人理财不可忽视的几件小事
  6. 云计算运维工程师好学吗?
  7. OpenLDAP中如何禁用账户,启用账户
  8. CSP 201912-3 化学方程式 100分
  9. 当前服务器ip配置文件,服务器远程ip配置文件
  10. java静态方法的调用方法_Java静态方法和实例方法