SlideShare a Scribd company logo
MockServer 的应⽤用与
实践
⻬齐磊
2016年12⽉月1⽇日
• 依赖http 接⼝口的应⽤用测试⾯面临的问题
• 常⽤用Mock Server框架
• WireMock 特性
• WireMock 实践
• 消费者驱动的契约测试(CDC)
• 使⽤用JVM-PACT 实现CDC
主要内容
依赖http 接⼝口的应⽤用测试⾯面
临的问题
• 第三⽅方接⼝口不可⽤用
• 如何测试失败的场景
• 如何测试超时
• 当服务A->B->C的时候,如何压测B
解决⽅方案-Mock Server框架
• Moco
• MockServer
• WireMock
⽐比较:
Moco是国⼈人开发的,简单易⽤用
MockServer 功能强⼤大,⽀支持Docker镜像
WireMock 使⽤用最多,Spring Cloud Contract 使⽤用
WireMock 特性
• 基于Http API的模拟器,设置请求响应的内容
• 可以作为类库,也可以作为单独进程运⾏行
• 所有的特性都可以通过Http Rest和java api调⽤用
• Stubs 能通过json⽂文件配置
• 记录并回放
使⽤用WireMock-1
使⽤用SPRING INITIALIZR 新建项⺫⽬目 http://
start.spring.io/
使⽤用WireMock-2
添加依赖
<dependency>
<groupId>com.github.tomakehurst</groupId>
<artifactId>wiremock</artifactId>
<version>2.3.1</version>
</dependency>
使⽤用WireMock-3
@Rule

public WireMockRule wireMockRule = new WireMockRule(9999);



@Test

public void testHi() {

stubFor(get(urlEqualTo("/api/hi"))

.willReturn(aResponse()

.withStatus(200)

.withBody("Hellow World")));



String response = restTemplate.getForObject("http://
localhost:9999/api/hi", String.class);

log.info("GET RESP:" + response);



assertThat(response).isEqualTo("Hellow World");

}
Demo
• WireMock Hello World(源码)
• Http Stub的响应放在⽂文件中
• 卖品中的WireMock
源码:https://github.com/qilei/wiremock-sample-boot
作为单独应⽤用启动
1. 下载jar,然后运⾏行
java -jar wiremock-standalone-2.4.1.jar
2. 通过http接⼝口配置
curl -X POST --data '{ "request": { "url": "/get/this", "method": "GET" }, "response":
{ "status": 200, "body": "Here it is!n" }}' 
http://localhost:8080/__admin/mappings/new
3. 访问
curl http://localhost:8080/get/this
接⼝口测试还有问题吗?
当⼀一个Service已经被多个使⽤用⽤用者调⽤用⽤用的时候
怎么保证
Service的修改不会对其他所有使⽤用者造成影响?
答案:契约测试 Contract Testing
契约测试的⼯工具
• Pact (https://github.com/realestate-com-au/pact)
• Spring Cloud Contract (https://cloud.spring.io/
spring-cloud-contract/)
Mock Server的应用与实践
源码:https://github.com/anha1/microservices-pact-maven
Demo:⽤用JVM-PACT 实现契约测试
# 参考
## WireMock
官⺴⽹网:http://wiremock.org/
WireMock Docs: http://wiremock.org/docs/
使⽤用WireMock模拟HTTP API服务: http://www.infoq.com/cn/articles/Wiremock-testing-mocking-over-wire-stubs
HTTPServerMock从⼿手⼯工到平台的演变: http://www.infoq.com/cn/articles/evolution-of-httpservermock-from-hand-to-platform
API Mocking⼯工具WireMock v2发布: http://www.infoq.com/cn/news/2016/08/wiremock-v2-api-mocking
API Mocking⼯工具⼤大全: http://blog.trafficparrot.com/2016/08/service-virtualization-and-stubbing.html
美团Vane 联调系统:http://www.infoq.com/cn/presentations/api-interface-and-data-mock-in-the-front-and-back-separation
## Contract Testing
PACT: https://docs.pact.io/
PACT-JVM: https://github.com/DiUS/pact-jvm
Consumer-Driven Contracts: http://martinfowler.com/articles/consumerDrivenContracts.html
Thanks!

More Related Content

PDF
WireMock 起飞手册
PPTX
Java API for WebSocket 實作介紹
PDF
PHP 應用之一 socket funion : 偽 WEB Server
PDF
用Maven管理專案的依賴關係
PPTX
Spring boot 简介
PPTX
实时Web的前世今生未来
PPT
Real time web实时信息流推送
PDF
Eventmachine Websocket 實戰
WireMock 起飞手册
Java API for WebSocket 實作介紹
PHP 應用之一 socket funion : 偽 WEB Server
用Maven管理專案的依賴關係
Spring boot 简介
实时Web的前世今生未来
Real time web实时信息流推送
Eventmachine Websocket 實戰

What's hot (18)

PDF
How to upgrade_ror3_to_ror4
PDF
twMVC#19 | opserver監控服務的解決
PPTX
How to ASP.NET MVC4
PDF
API Mocking
PDF
广告技术部自动化测试介绍.pdf
PPTX
Asp.net mvc 概觀介紹
PDF
Leverage virtualization in software test
PDF
Vlog02 [eng sub]什麼是controller和如何在asp.net核心中創建controller?-what is controller ...
PPTX
利用Signalr打造即時通訊@Tech day geek
PPTX
Asp.net core v1.0
PPTX
Build your ETL job using Jenkins - step by step
PPTX
ASP.NET 開發人員不可不知的 IIS (IIS for ASP.NET Developers)
PDF
ASP.NET MVC Web API -twMVC#8
PPTX
real-time Web的運用
PPTX
Asp.net mvc 6 新功能初探
PDF
Vagrant教學
PDF
twPolitwoops final
PPT
Php Webservers
How to upgrade_ror3_to_ror4
twMVC#19 | opserver監控服務的解決
How to ASP.NET MVC4
API Mocking
广告技术部自动化测试介绍.pdf
Asp.net mvc 概觀介紹
Leverage virtualization in software test
Vlog02 [eng sub]什麼是controller和如何在asp.net核心中創建controller?-what is controller ...
利用Signalr打造即時通訊@Tech day geek
Asp.net core v1.0
Build your ETL job using Jenkins - step by step
ASP.NET 開發人員不可不知的 IIS (IIS for ASP.NET Developers)
ASP.NET MVC Web API -twMVC#8
real-time Web的運用
Asp.net mvc 6 新功能初探
Vagrant教學
twPolitwoops final
Php Webservers
Ad

Similar to Mock Server的应用与实践 (20)

PDF
Btrace intro(撒迦)
PDF
Nodejs & NAE
PDF
美团前端架构简介
PPTX
twMVC#31網站上線了然後呢
PDF
05.wls调优
PPT
新浪云计算公开课第一期:Let’s run @ sae(丛磊)
PPTX
谈谈模块化
PDF
Truck js 高性能移动web开发解决方案
PDF
从CI到CD[麻袋理财王天青]v1
PDF
通用Mock平台在支付宝测试的实践
PDF
Meteor
PDF
钱宝坤:多浏览器集成的JavaScript单元测试工具
PPT
Traffic server overview
PDF
Ch13 整合Spring MVC
PPTX
網路技術心得分享
PPT
从林书豪到全明星 - 虎扑网技术架构如何化解流量高峰
KEY
新浪微博平台与安全架构
PDF
WoT Frotend 的設計與實作
PPT
构建可扩展的微博系统
PPT
java title
Btrace intro(撒迦)
Nodejs & NAE
美团前端架构简介
twMVC#31網站上線了然後呢
05.wls调优
新浪云计算公开课第一期:Let’s run @ sae(丛磊)
谈谈模块化
Truck js 高性能移动web开发解决方案
从CI到CD[麻袋理财王天青]v1
通用Mock平台在支付宝测试的实践
Meteor
钱宝坤:多浏览器集成的JavaScript单元测试工具
Traffic server overview
Ch13 整合Spring MVC
網路技術心得分享
从林书豪到全明星 - 虎扑网技术架构如何化解流量高峰
新浪微博平台与安全架构
WoT Frotend 的設計與實作
构建可扩展的微博系统
java title
Ad

Mock Server的应用与实践