使用mockserver来进行http接口mock
转载自:https://blog.csdn.net/heymysweetheart/article/details/52227379;(注,这个不是很符合我的要求,它主要的作用是可以通过简单的代码就能filter对方的请求options,然后做出对应的响应;而我需要的是一个能够开启为http服务端来测试客户端发来的http请求的工具,然后顺便看到了这篇感觉以后可能也会用到)
前言
进行单元测试时,必须要mock掉第三方的依赖调用,而mockserver提供了足够的api来支持这种http的mock,现在简单介绍如何使用mockserver进行http接口mock。
依赖
- mockserver依赖
<dependency><groupId>org.mock-server</groupId><artifactId>mockserver-netty</artifactId><version>3.10.4</version></dependency>
- httpclient依赖
<dependency><groupId>org.apache.httpcomponents</groupId><artifactId>httpclient</artifactId><version>4.3.3</version></dependency><!-- https://mvnrepository.com/artifact/org.apache.httpcomponents/httpcore --><dependency><groupId>org.apache.httpcomponents</groupId><artifactId>httpcore</artifactId><version>4.3.2</version></dependency>
httpclient的版本可能会出现不一致的情况,抛出异常,以上的这个版本匹配是没有异常的组合。
举例
package com.yuliang.dubbo.service;import org.apache.http.client.methods.CloseableHttpResponse;import org.apache.http.client.methods.HttpGet;import org.apache.http.impl.client.CloseableHttpClient;import org.apache.http.impl.client.HttpClients;import org.junit.Rule;import org.junit.Test;import org.mockserver.client.server.MockServerClient;import org.mockserver.junit.MockServerRule;import java.io.BufferedReader;import java.io.IOException;import java.io.InputStream;import java.io.InputStreamReader;import static org.hamcrest.CoreMatchers.equalTo;import static org.hamcrest.MatcherAssert.assertThat;import static org.mockserver.model.HttpRequest.request;import static org.mockserver.model.HttpResponse.response;/*** Created by leo on 16/8/16.*/public class MockServerTest {@Rulepublic MockServerRule server = new MockServerRule(this, 5000);@Testpublic void testMockServer() throws IOException {MockServerClient mockClient = new MockServerClient("localhost", 5000);String expected = "{ message: 'incorrect username and password combination' }";mockClient.when(request().withPath("/hello/John").withMethod("GET")// .withHeader(new Header(HttpHeaders.ACCEPT, MediaType.TEXT_PLAIN))// .withQueryStringParameter(new Parameter("my-token", "12345"))).respond(response().withStatusCode(200).withBody(expected));CloseableHttpClient client = HttpClients.createDefault();HttpGet httpGet = new HttpGet("http://localhost:5000/hello/John");CloseableHttpResponse response = client.execute(httpGet);//验证输出是否是正确InputStream content = response.getEntity().getContent();InputStreamReader inputStreamReader = new InputStreamReader(content);BufferedReader bufferedReader = new BufferedReader(inputStreamReader);String responseText = bufferedReader.readLine();assertThat(responseText, equalTo(expected));}}
mock http post请求
@Testpublic void testPostMockServer() throws IOException {MockServerClient mockClient = new MockServerClient("localhost", 5000);String expected = "You have logged in successfully.";mockClient.when(request().withPath("/hello/John").withMethod("POST").withHeader(new Header(HttpHeaders.ACCEPT, MediaType.TEXT_PLAIN))// .withQueryStringParameter(new Parameter("my-token", "12345")).withBody("username=foo&password=123456")).respond(response().withStatusCode(200).withBody(expected));CloseableHttpClient client = HttpClients.createDefault();HttpPost httpPost = new HttpPost("http://localhost:5000/hello/John");httpPost.addHeader("Accept", "text/plain");ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();nameValuePairs.add(new BasicNameValuePair("username", "foo"));nameValuePairs.add(new BasicNameValuePair("password", "123456"));httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));CloseableHttpResponse response = client.execute(httpPost);//验证输出是否是正确InputStream content = response.getEntity().getContent();InputStreamReader inputStreamReader = new InputStreamReader(content);BufferedReader bufferedReader = new BufferedReader(inputStreamReader);String responseText = bufferedReader.readLine();assertThat(responseText, equalTo(expected));}
使用mockserver来进行http接口mock的更多相关文章
- 使用electron开发一个h5的客户端应用创建http服务模拟后台接口mock
使用electron开发一个h5的客户端应用创建http服务模拟后端接口mock 在上一篇<electron快速开始>里讲述了如何快速的开始一个electron的应用程序,既然electr ...
- RPC接口mock测试
转载:http://blog.csdn.net/ronghuanye/article/details/71124127 1 简介 Dubbo目前的应用已经越来越广泛.或者基于Dubbo二 ...
- rpc接口mock平台
转载:http://blog.csdn.net/ronghuanye/article/details/71124320 1.简介 平台采用struts.spring.mybatis框架开发设计,主要用 ...
- anyproxy学习2-rule模块实现接口mock功能
前言 AnyProxy不仅仅可以抓包,还可以拦截请求并修改服务端响应,实现接口mock功能. 面试时候经常会问到第三方支付如何测试这种,如果对接的第三方没提供测试环境,那么就需要搭建一个mock服务器 ...
- 努力一周,开源一个超好用的接口Mock工具——Msw-Tools
作为一名前端开发,是不是总有这样的体验:基础功能逻辑和页面UI开发很快速,本来可以提前完成,但是接口数据联调很费劲,耗时又耗力,有时为了保证进度还不得不加加班. 为了摆脱这种痛苦,经过一周的努力,从零 ...
- RAP 接口Mock示例
前后端分离式开发的思考 目前大部分公司都实行了前后端分离开发.然而在项目开发过程当中,经常会遇到以下几个尴尬的场景: 1.前端开发依赖于后端接口数据,需要与后端接口联调才能获得数据展示,从而拖慢了开发 ...
- Doclever 接口mock 操作
查看 接口项目mock 查看说明 假设 本机项目路径为 http://localhost:8080 >> 启动 node node net.js http://org.my.com/mo ...
- fiddler实现后端接口 mock(不需要修改开发代码)
转载:http://blog.csdn.net/huazhongkejidaxuezpp/article/details/50435552 步骤 1. 获取 接口 定义(接口返回的json串) ...
- SpringBoot开发mockserver及生成swagger接口文档
通过springboot开发mock server,包含get及post接口,用于练习接口自动化及jmeter很方便 当然,也为后面jenkins持续集成做基础(开发push代码后 → jenkin ...
随机推荐
- JDK1.5 Excutor 与ThreadFactory
Excutor 源码解读: /** * An object that executes submitted {@link Runnable} tasks. This * interface provi ...
- 2017-2018-2 20165315 实验三《敏捷开发与XP实践》实验报告
2017-2018-2 20165315 实验三<敏捷开发与XP实践>实验报告 一.编码标准 编写代码一个重要的认识是"程序大多时候是给人看的",编程标准使代码更容易阅 ...
- first H5
<!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content ...
- rbac 表结构的。设计
1. 问:为什么程序需要权限控制? 答:生活中的权限限制,① 看灾难片电影<2012>中富人和权贵有权登上诺亚方舟,穷苦老百姓只有等着灾难的来临:② 屌丝们,有没有想过为什么那些长得漂亮身 ...
- Java中重写与重载的区别
方法重载:关键字overload,方法名和方法的返回类型都相同,方法参数个数和类型不一样方法重写:也叫方法覆盖,关键字override,相对于类继承而言,重写的方法名,返回类型,参数个数,参数类型都要 ...
- c# mac地址 和http://xx.xx.xx/ 正则表达式匹配
Mac :^([0-9a-fA-F]{2})(([/\s:][0-9a-fA-F]{2}){5})$ C# 书写方式 一下是允许mac中间间隔符是“:”或者“-”两种输入方式 并且我把上边的正则表达 ...
- HTML知识基础
HTML 超文本标记语言(Hyper Text Markup Language):是一种用于创建网页的标准标记语言. Hyper Text:指具有交互功能文本. Markup Language: ...
- Liunx Mkdir
linux mkdir命令: 创建目录 介绍:该命令创建指定的目录名,要求创建目录的用户在当前目录中具有写权限,并且指定的目录名不能是当前目录中已有的目录1语法: mkdir [-m] [-p] 目录 ...
- 日期控件 DatePicker 在ie8不能用
过个年,日期控件DatePicker在ie8下突然不能用了,程序也没升级,很是奇怪. 把ie8的“禁用脚本调试”去掉,再次运行,发现提示有脚本错误. 想着可能是兼容性问题,于是把兼容性视图打开运行,还 ...
- sql 查询某个字段出现的次数
表名随便起个 testtable 那么有这么一个需求,利用你所学的sql语句 单表查询出下表的结果 也就是统计某个时间某个值出现的次数其实一开始我是很懵,毕竟之前也没做过,只能怪自己学得太浅了.过后我 ...