第7章mock平台功能介绍
1、moco框架,下载standalone.jar,最大的包
https://repo1.maven.org/maven2/com/github/dreamhead/moco-runner/1.0.0/


2、在jar文件同级目录下创建一个.json文件

3、test.json内容必须是一个数组([])开头,内部是json格式,一个json就等于一个请求。如果有多个请求就创建多个json,都是放在数组里面,基本格式:

moco响应信息会乱码,需要在响应信息头加上: “Content-Type”:“text/html;charset=gbk”。

description:接口的说明,给自己看的。

response:请求的信息。

uri:接口的路径。

method:方法类型

json:请求需要传的参数。

headers:设置请求头。

response:设置响应的信息。

text:响应的内容,这个只能返回文本,不能返回json。

json:设置响应内容为json格式的数据。

headers:响应的头,例如: “Content-Type”:“text/html;charset=gbk”。

这是一个简单的moco示例

[{"description":"这是一个mock测试的接口","request":{"uri":"/demo","method":"get"},"response":{"text":"这是mock响应的结果"}}
]

4、在dos命令行输入:java -jar moco-runner-1.0.0-standalone.jar http -p 8888 -c mockTest.json

java -jar mocojar的名称 http -p 端口-c json文件的名称

如果出现一下内容就是启动成功:

在浏览器中输入
http://127.0.0.1:8888/demo或
http://localhost:8888/demo,可以成功访问


5、带参get

[{"description":"这是一个mock测试的接口","request":{"uri":"/getdemo","method":"get"},"response":{"text":"这是无参数的get请求"}},{"description":"这是一个带参的get请求","request":{"uri":"/getwithparam","method":"get","queries": {"name": "lily","age": "18"}},"response":{"text":"这是带参数的get请求"}}
]


带参post

[{"description":"这是一个post请求","request":{"uri":"/postdemo","method":"post"},"response":{"text":"这是mock的post请求"}},{"description":"这是手机号错误的接口","request":{"uri":"/loginPhoneError","method":"post","headers":{"Content-Type":"application/json"},"json":{"phone":"15519094256","password":"osm6762955"}},"response":{"json":{"msg":"手机号或密码错误","code":500,"data":null},"headers":{"Content-Type":"text/html;charset=utf-8"}}}
]

第一个demo

public class MyHttpClient {public static void main(String[] args) throws IOException {//用来存放结果String result;HttpGet get = new HttpGet("Http://www.baidu.com");HttpClient httpClient = HttpClients.createDefault();HttpResponse httpResponse = httpClient.execute(get);System.out.println("响应"+httpResponse);//获取响应结果result = EntityUtils.toString(httpResponse.getEntity(),"utf-8");System.out.println("响应result"+result);}
}

携带cookies信息访问get请求

import org.apache.http.HttpResponse;
import org.apache.http.client.CookieStore;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.cookie.Cookie;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.testng.annotations.Test;import java.io.IOException;
import java.util.List;public class MyCookieForGet {//用来存储cookies变量private CookieStore store;@Testpublic void cookies() throws IOException {//用来存放结果String result;HttpGet get = new HttpGet("Http://www.baidu.com");DefaultHttpClient client = new DefaultHttpClient();CloseableHttpResponse response = client.execute(get);//获取响应状态码System.out.println(response.getStatusLine().getStatusCode());//获取响应结果result = EntityUtils.toString(response.getEntity(),"utf-8");System.out.println("响应result"+result);//获取cookies信息this.store = client.getCookieStore();List<Cookie> cookieList = store.getCookies();for (Cookie cookie : cookieList) {String name = cookie.getName();String value = cookie.getValue();System.out.println("cookie name:"+name);System.out.println("cookie value:"+value);}System.out.println("======");}@Test(dependsOnMethods = {"cookies"})public void testGetWithCookies() throws IOException {HttpGet get = new HttpGet("Http://www.baidu.com/xxx");DefaultHttpClient client = new DefaultHttpClient();//设置cookie信息client.setCookieStore(this.store);CloseableHttpResponse response = client.execute(get);}
}

携带cookies访问post请求

import org.apache.http.client.CookieStore;
import org.apache.http.client.HttpClient;
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.cookie.Cookie;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.json.JSONObject;
import org.testng.annotations.Test;import java.io.IOException;
import java.util.List;
import java.util.ResourceBundle;public class MyCookiesForPost {private String url;private ResourceBundle bundle;//用来存储cookies变量private CookieStore store;@Testpublic void BeforeTest(){bundle= ResourceBundle.getBundle("application.properties");url=bundle.getString("test.url");}@Testpublic void cookies() throws IOException {//用来存放结果String result;HttpGet get = new HttpGet("Http://www.baidu.com");DefaultHttpClient client = new DefaultHttpClient();CloseableHttpResponse response = client.execute(get);//获取响应状态码System.out.println(response.getStatusLine().getStatusCode());//获取响应结果result = EntityUtils.toString(response.getEntity(),"utf-8");System.out.println("响应result"+result);//获取cookies信息this.store = client.getCookieStore();List<Cookie> cookieList = store.getCookies();for (Cookie cookie : cookieList) {String name = cookie.getName();String value = cookie.getValue();System.out.println("cookie name:"+name);System.out.println("cookie value:"+value);}System.out.println("======");}@Test(dependsOnMethods = {"cookies"})public void testPostMethod() throws IOException {String uri=bundle.getString("test.post.with.cookies");//拼接最终的测试地址String testUrl = this.url + uri;//声明一个Client对象,用来进行方法的执行DefaultHttpClient client = new DefaultHttpClient();//声明一个方法,就是post方法HttpPost post = new HttpPost(testUrl);//添加参数JSONObject param = new JSONObject();param.put("name","xiaowang");param.put("age","18");//设置请求头信息post.setHeader("content-type","application/json");//将参数添加到方法中StringEntity entity = new StringEntity(param.toString(),"utf-8");post.setEntity(entity);//声明一个对象来进行响应结果的存储String result;//设置cookies信息client.setCookieStore(this.store);//执行post方法CloseableHttpResponse response = client.execute(post);//获取响应结果result = EntityUtils.toString(response.getEntity(), "utf-8");//处理结果,转化为json字符串JSONObject resultJson = new JSONObject(result);//判断结果String status = (String)resultJson.get("status");}
}

第10章,springBoot 简介及官方demo开发
1、配置pom文件

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>org.example</groupId><artifactId>chapter10</artifactId><version>1.0-SNAPSHOT</version><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.0.0.RELEASE</version></parent><dependencies><dependency><groupId>org.apache.httpcomponents</groupId><artifactId>httpclient</artifactId><version>4.5.13</version></dependency><dependency><groupId>org.testng</groupId><artifactId>testng</artifactId><version>7.6.1</version></dependency><dependency><groupId>org.json</groupId><artifactId>json</artifactId><version>20170516</version></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency></dependencies></project>

开发代码示例,运行后在浏览器输入http://localhost:8080/ 可以查看到内容hello world

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;@Controller
@EnableAutoConfiguration
public class SimpleController {@RequestMapping("/")@ResponseBodyString home(){return "hello world";}public static void main(String[] args) {SpringApplication.run(SimpleController.class,args);}}

2、返回cookies信息的接口开发

3&4携带cookies信息访问的get接口开发

5、携带参数get请求开发方式一

携带参数get请求开发方式二

7、返回cookie信息的post接口开发

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;@SpringBootApplication
@ComponentScan("com.course.server")
public class Application {public static void main(String[] args) {SpringApplication.run(Application.class,args);}
}
package com.course.server;import org.springframework.web.bind.annotation.*;import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;@RestController
public class MyGetMethod {@RequestMapping(value = "/getCookies",method = RequestMethod.GET)public String getCookies(HttpServletResponse response){Cookie cookie = new Cookie("login", "true");response.addCookie(cookie);return "恭喜你获得cookies信息";}/*** 要求客户端携带cookies访问*/@RequestMapping(value = "get/with/cookies",method = RequestMethod.GET)public String getWithCookies(HttpServletRequest request){Cookie[] cookies = request.getCookies();if (Objects.isNull(cookies)){return "需要携带cookies";}for (Cookie cookie : cookies) {if (cookie.getName().equals("login")&&cookie.getValue().equals("true")){return "携带cookies访问成功";}}return "必须携带cookies信息来";}/*** 开发一个需要携带参数才能访问的get请求* 第一种实现方式url:key=value&key=value*/@RequestMapping(value = "/get/with/param",method = RequestMethod.GET)public Map<String,Integer> getList(@RequestParam Integer start, @RequestParam Integer end){HashMap<String, Integer> myList = new HashMap<>();myList.put("鞋",400);myList.put("面",1);myList.put("T shirt",200);return myList;}/*** 第二种携带参数访问的get请求* url:ip:port//get/with/param/10/20*/@RequestMapping(value = "/get/with/param/{start}/{end}",method = RequestMethod.GET)public  Map myGetList(@PathVariable Integer start,@PathVariable Integer end){HashMap<String, Integer> myList = new HashMap<>();myList.put("鞋",400);myList.put("面",1);myList.put("T shirt",200);return myList;}
}
package com.course.server;import com.course.bean.User;
import org.springframework.web.bind.annotation.*;import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;@RestController
@RequestMapping("v1")
public class MyPostMethod {//这个变量用来装cookies信息private static Cookie cookie;//用户登录成功获取cookies,然后在访问其他接口@RequestMapping(value = "/login",method = RequestMethod.POST)public  String login(HttpServletResponse response,@RequestParam(value = "userName",required = true) String userName,@RequestParam(value = "password",required = true) String password){if (userName.equals("zhangsan")&&password.equals("123456")){Cookie cookie = new Cookie("login","true");response.addCookie(cookie);return "恭喜登录成功";}return "用户名或密码错误";}@RequestMapping(value = "/getUserList",method = RequestMethod.POST)public User getUserList(HttpServletRequest request,@RequestBody User u){User user;//获取cookiesCookie[] cookies = request.getCookies();//验证cookies是否合法for (Cookie c : cookies) {if (c.getName().equals("login") && c.getValue().equals("true")&& u.getUsername().equals("zhangsan")&& u.getPassword().equals("123456")){user = new User();user.setName("lisi");user.setAge("18");user.setSex("man");return user;}}return null;}}

java 接口自动化相关推荐

  1. Java接口自动化之TestNG单元测试框架(一)

    上一篇Java接口自动化系列文章:Java接口自动化之log4j日志框架,主要介绍log4j日志介绍.日志三大组成部分及日志实战. 以下主要介绍TestNG的简介.@Test注解及其属性. 01 Te ...

  2. Java接口自动化之Maven工具使用

    VOL 190 30 2020-12 今天距2021年1天 这是ITester软件测试小栈第190次推文 点击上方蓝字"ITester软件测试小栈"关注我,每周一.三.五早上 08 ...

  3. java接口自动化(四) - 企业级代码管理工具Git的应用

    1.简介 首先我们自己需要将自己的代码上传到GitHub上边做好备份.用来避免万一由于某些不可控的非人为因素或者人为因素造成的代码丢失.而且GitHub是一个开源的代码管理工具.所以宏哥这里再次介绍一 ...

  4. java接口自动化书籍_java接口自动化优化(一)

    优化extentreports在线样式改为离线加载自己项目下的样式 主要解决extentreports在线加载失败问题 上篇文章介绍了通过testng编写用例后使用extentreports作为测试报 ...

  5. java接口自动化Excel占位符_基于maven+java+TestNG+httpclient+poi+jsonpath+ExtentReport的接口自动化测试框架...

    接口自动化框架 项目说明 本框架是一套基于maven+java+TestNG+httpclient+poi+jsonpath+ExtentReport而设计的数据驱动接口自动化测试框架,TestNG ...

  6. java接口自动化(三) - 手工接口测试到自动化框架设计之鸟枪换炮

    1.简介 上一篇宏哥介绍完了接口用例设计,那么这一章节,宏哥就趁热打铁介绍一下,接口测试工具.然后小伙伴们或者童鞋们就可以用接口测试工具按照设计好的测试用例开始执行用例进行接口手动测试了.关于手动测试 ...

  7. java接口自动化(一) - 接口自动化测试整体认知 - 开山篇(超详解)

    简介 了解什么是接口和为什么要做接口测试.并且知道接口自动化测试应该学习哪些技术以及接口自动化测试的落地过程.其实这些基本上在python接口自动化的文章中已经详细的介绍过了,不清楚的可以过去看看.了 ...

  8. java接口自动化监控_java接口自动化(三) - 手工接口测试到自动化框架设计之鸟枪换炮...

    1.简介 上一篇宏哥介绍完了接口用例设计,那么这一章节,宏哥就趁热打铁介绍一下,接口测试工具.然后小伙伴们或者童鞋们就可以用接口测试工具按照设计好的测试用例开始执行用例进行接口手动测试了.关于手动测试 ...

  9. 阿里java接口自动化实践pdf_接口自动化测试设计

    1.接口测试基础 1.1.什么是接口测试? 接口测试是测试系统组件间接口的一种测试. 接口测试主要用于检测外部系统与系统之间以及内部各个子系统之间的交互点. 接口测试的重点是要检查数据的交换,传递和控 ...

  10. Java接口自动化框架系列07:Web接口自动化测试框架设计思路

    1.Java web接口自动化框架 框架名称:Java+Maven+httpClients+TestNg+Allure (因本次只讲解java部分,未包括git和jenkins,如果是包括git和je ...

最新文章

  1. matplotlib库之patch
  2. Git(一)之基本操作详解
  3. 2020兰大计算机学硕线,兰州大学2020年硕士研究生招生复试分数线
  4. 日常生活小技巧 -- markdown编辑器
  5. 时代亿信 文件共享访问控制网关
  6. netdev: dev_watchdog timer(结合stmmac 分析)
  7. c语言实验报告熟悉vc,C语言实验报告源代码
  8. request.getContextPath()取不到值
  9. 2.2 理解 mini-batch 梯度下降法
  10. 1、Keepalived及VRRP原理介绍
  11. hadoop-2.5安装与配置
  12. OpenCV 实现颜色直方图
  13. 勒索病毒记录:将电脑 后缀改为.nedjprf
  14. Codechef Black Nodes in Subgraphs(树型背包)
  15. 树莓派usb免驱摄像头报错
  16. 学生通讯录管理系统的设计与实现
  17. matlab求二项分布/指数分布的期望与方差
  18. 《活着》的优秀读后感范文3000字
  19. 第6节 远程管理路由器及交换机—基于Cisco Packet Tracer
  20. 科技视界杂志科技视界杂志社科技视界编辑部2022年第21期目录

热门文章

  1. Integer 用 == 比较时 127 相等 128 不相等
  2. 金庸笔下人物以及网络俏皮英语对应关系表-诗词
  3. C#实现将度分秒化为弧度值
  4. ID基本操作(标尺,参考线,网格)5.11
  5. Docker通过DockerFile自定义Centos 镜像
  6. 动手深度学习13:计算机视觉——语义分割、风格迁移
  7. 2022-2028全球自动超声波仪器清洗器行业调研及趋势分析报告
  8. Filter过滤器(超详细)
  9. 开车的26条教训!开车的人一定看看!
  10. PointNet: Deep Learning on Point Sets for 3D Classification and Segmentation