文章目录

  • 一、引言
  • 二、简介
  • 三、原理
  • 四、使用
    • 1、约定请求 URI
    • 2、约定请求 Queries
    • 3、约定请求 Get 方法
    • 4、约定请求 Post 方法
    • 5、约定请求 Headers
    • 6、约定请求 Cookies
    • 7、约定请求 Forms
    • 8、约定请求 URI (Match)
    • 9、约定请求 URI (StartsWith)
    • 10、约定请求 URI (endsWith)
    • 11、约定请求 URI (Contain)
    • 12、约定指定 Json 响应
    • 13、约定响应 Status
    • 14、约定响应 Headers
    • 15、约定响应 Cookies
    • 16、约定重定向 RedirectTo
  • 五、小结

一、引言

在上文中,我们介绍 Mock 的基本概念,本文我们将详细介绍其中一个快速简单Mock Server Moco

二、简介

简单来说 Moco 就是类似一个 Mock 的工具框架,一个简单搭建模拟服务器的程序库 / 工具,下载就是一个JAR包。
在 Moco 的 github 上面有这段话。

Integration, especially based on HTTP protocol, e.g. web service, REST etc, is wildly used in most of our development.
In the old days, we just deployed another WAR to an application server, e.g. Jetty or Tomcat etc. As we all know, it’s so boring to develop a WAR and deploy it to any application server, even if we use an embeded server. And the WAR needs to be reassembled even if we just want to change a little bit.

翻译过来:

集成,特别是基于 HTTP 协议的集成,例如 web 服务、REST 等,在我们的大多数开发中都被广泛使用。
在过去,我们只是将另一场 WAR 包部署到应用服务器上,例如 Jetty 或Tomcat 等。众所周知,开发一个 WAR 包并将其部署到任何应用服务器上是非常枯燥的,即使我们使用的是嵌入式服务器。war包也需要被重新打包即使我们只是想稍微改变一下。
简单来说,Moco 就是解决了开发前端时没有后端支持,开发接口时依赖没有到位的尴尬场景。当然 Moco 的灵活性,让其有越来越多的应用场景,比如我们在开发接口测试的时候。

特点:

  • 只需要简单的配置 request、response 等即可满足要求,支持 http、https、socket 。可以说是非常的灵活性。
  • 支持在 request 中设置 Headers , Cookies , StatusCode 等。
  • 对 GET、POST、PUT、DELETE 等请求方式均支持,很适合 web 开发。
  • 无需环境配置,有 Java 环境即可。
  • 修改配置后,立刻生效。只需要维护接口,也就是契约即可。
  • 对可能用到的数据格式都支持,如 Json、text、xml、file 等。
  • 还能与其他工具集成,如 Junit、Maven、Gradle 等。

三、原理

Moco 本身支持 API 和独立运行两种方式。通过 API ,开发人员可以在Junit、TestNg 等测试框架里使用 Moco,这样极大地降低了接口测试的复杂度。
Moco 根据一些配置,启动一个真正的 HTTP 服务(监听本地指定端口)。当发起的请求满足一个条件时,就会收到一个 response 。Moco 底层并没有依赖于像 Servlet 这样的重型框架,而是基于 Netty 的网络应用框架编写的,这样就绕过了复杂的应用服务器,所以它的速度是极快的。

四、使用

加载配置启动 Moco HTTP Server

java -jar <moco-runner-path> http -p <port> -c <configfile-path>

启动命令参数含义:

  • moco-runner-pathmoco-runner-0.11.0-standalone.jar 包路径。
  • port :HTTP 服务监听端口。
  • configfile-path :配置文件路径

下面介绍不同的 HTTP 服务,以及如何设置 JSON 文件的参数

在本地启动一个 http 服务器,其中监听端口是 12306,配置文件是 JSON 文件。只需要本机发起一个request,如:http://localhost:12306

1、约定请求 URI

JSON 脚本

[{"description":"这是一个请求URI","request":{"uri":"/7d"},"response":{"text":"success!"}}
]

启动命令

java -jar moco-runner-0.11.0-standalone.jar http -p 12306 -c ./src/main/resources/startupURI.json

通过 Postman 验证服务,测试 Get 请求

Moco 服务日志

09 十二月 2018 11:06:50 [nioEventLoopGroup-3-2] INFO  Request received:GET /7d HTTP/1.1
Host: 127.0.0.1:12306
User-Agent: PostmanRuntime/7.4.0
Accept: */*
Accept-Encoding: gzip, deflate
Cache-Control: no-cache
Postman-Token: 7b5a1a47-a287-4674-b94e-c455fc5c645a
X-Lantern-Version: 5.1.0
Content-Length: 009 十二月 2018 11:06:50 [nioEventLoopGroup-3-2] INFO  Response return:HTTP/1.1 200
Content-Length: 10
Content-Type: text/plain; charset=utf-8success!

2、约定请求 Queries

JSON 脚本

[{"description":"这是一个请求queries","request":{"uri":"/7d","queries":{"name":"zuozewei"}},"response":{"text":"success!"}}
]

通过 Postman 验证服务,测试 Get 请求

Moco 服务日志

09 十二月 2018 11:21:04 [nioEventLoopGroup-3-2] INFO  Request received:GET /7d HTTP/1.1
Host: 127.0.0.1:12306
User-Agent: PostmanRuntime/7.4.0
Accept: */*
Accept-Encoding: gzip, deflate
Cache-Control: no-cache
Postman-Token: 2d36e386-e022-4478-8acd-258eff4ff684
X-Lantern-Version: 5.1.0
Content-Length: 009 十二月 2018 11:21:04 [nioEventLoopGroup-3-2] INFO  Response return:HTTP/1.1 200
Content-Length: 10
Content-Type: text/plain; charset=utf-8success!

3、约定请求 Get 方法

JSON 脚本

[{"description":"这是一个get请求","request":{"uri":"/7d","method":"get"},"response":{"text":"success!"}}
]

通过 Postman 验证服务,测试 Get 请求

Moco 服务日志

09 十二月 2018 11:26:42 [nioEventLoopGroup-3-2] INFO  Request received:GET /7d HTTP/1.1
Host: 127.0.0.1:12306
User-Agent: PostmanRuntime/7.4.0
Accept: */*
Accept-Encoding: gzip, deflate
Cache-Control: no-cache
Postman-Token: ae3250b6-0ec0-4875-8970-d37e5b840820
X-Lantern-Version: 5.1.0
Content-Length: 009 十二月 2018 11:26:42 [nioEventLoopGroup-3-2] INFO  Response return:HTTP/1.1 200
Content-Length: 10
Content-Type: text/plain; charset=utf-8success!

4、约定请求 Post 方法

JSON 脚本

[{"description":"这是一个post请求","request":{"uri":"/7d","method":"post"},"response":{"text":"success!"}}
]

通过 Postman 验证服务,测试 Post 请求

Moco 服务日志

09 十二月 2018 11:29:30 [nioEventLoopGroup-3-2] INFO  Request received:POST /7d HTTP/1.1
Host: 127.0.0.1:12306
User-Agent: PostmanRuntime/7.4.0
Content-Length: 0
Accept: */*
Accept-Encoding: gzip, deflate
Cache-Control: no-cache
Postman-Token: 73f38af1-4efb-473a-b9d2-de0392c65bbe
X-Lantern-Version: 5.1.009 十二月 2018 11:29:30 [nioEventLoopGroup-3-2] INFO  Response return:HTTP/1.1 200
Content-Length: 10
Content-Type: text/plain; charset=utf-8success!

5、约定请求 Headers

JSON 脚本

[{"description":"这是一个带headers的post请求","request":{"uri":"/7d","method":"post","headers":{"content-type":"application/json"}},"response":{"text":"success!"}}
]

通过 Postman 验证服务,测试 Post 请求

Moco 服务日志

09 十二月 2018 11:34:43 [nioEventLoopGroup-3-2] INFO  Request received:POST /7d HTTP/1.1
Host: 127.0.0.1:12306
User-Agent: PostmanRuntime/7.4.0
Content-Length: 0
Accept: */*
Accept-Encoding: gzip, deflate
Cache-Control: no-cache
Content-Type: application/json
Postman-Token: 0a82d74b-303f-42a3-9da0-32fd6c604166
X-Lantern-Version: 5.1.009 十二月 2018 11:34:43 [nioEventLoopGroup-3-2] INFO  Response return:HTTP/1.1 200
Content-Length: 10
Content-Type: text/plain; charset=utf-8success!

6、约定请求 Cookies

JSON 脚本

[{"description":"这是一个带cookies的post请求","request":{"uri":"/7d","method":"post","cookies":{"login":"7dgroup"}},"response":{"text":"success!"}}
]

通过 Postman 验证服务,测试 Post 请求


Moco 服务日志

09 十二月 2018 12:26:46 [nioEventLoopGroup-3-3] INFO  Request received:POST /7d HTTP/1.1
Host: 127.0.0.1:12306
User-Agent: PostmanRuntime/7.4.0
Content-Length: 0
Accept: */*
Accept-Encoding: gzip, deflate
Cache-Control: no-cache
Cookie: login=7dgroup
Postman-Token: 36a12412-6eb1-44a4-a2d8-ea222eba8968
X-Lantern-Version: 5.1.009 十二月 2018 12:26:46 [nioEventLoopGroup-3-3] INFO  Response return:HTTP/1.1 200
Content-Length: 10
Content-Type: text/plain; charset=utf-8success!

7、约定请求 Forms

JSON 脚本

[{"description":"这是一个带forms参数的post请求","request":{"uri":"/7d","method":"post","forms":{"name":"zuozewei"}},"response":{"text":"success!"}}
]

通过 Postman 验证服务,测试 Post 请求

Moco 服务日志

09 十二月 2018 12:50:47 [nioEventLoopGroup-3-3] INFO  Request received:POST /7d HTTP/1.1
Host: 127.0.0.1:12306
User-Agent: PostmanRuntime/7.4.0
Content-Length: 167
Accept: */*
Accept-Encoding: gzip, deflate
Cache-Control: no-cache
Content-Type: multipart/form-data; boundary=--------------------------977669308391204172275520
Postman-Token: 308d06bf-c110-4736-9ac4-ee2fe8a4a036
X-Lantern-Version: 5.1.0<content is binary>09 十二月 2018 12:50:47 [nioEventLoopGroup-3-3] INFO  Response return:HTTP/1.1 200
Content-Length: 10
Content-Type: text/plain; charset=utf-8success!

8、约定请求 URI (Match)

对于 Restful 风格的 url ,支持正则匹配。
JSON 脚本

[{"description":"这是一个请求Match URI","request":{"uri":{"match":"/\\w*/7d"}},"response":{"text":"success!"}}
]

通过 Postman 验证服务,测试 Post 请求

Moco 服务日志

09 十二月 2018 13:05:48 [nioEventLoopGroup-7-2] INFO  Request received:POST /wzuozewei/7d HTTP/1.1
Host: 127.0.0.1:12306
User-Agent: PostmanRuntime/7.4.0
Content-Length: 0
Accept: */*
Accept-Encoding: gzip, deflate
Cache-Control: no-cache
Content-Type: multipart/form-data; boundary=--------------------------767805110351846142172059
Postman-Token: 5d7b5c65-1f8b-46ae-8868-62def1a5de31
X-Lantern-Version: 5.1.009 十二月 2018 13:05:48 [nioEventLoopGroup-7-2] INFO  Response return:HTTP/1.1 200
Content-Length: 10
Content-Type: text/plain; charset=utf-8success!

9、约定请求 URI (StartsWith)

JSON 脚本

[{"description":"这是一个请求StartsWith URI","request":{"uri":{"startsWith":"/7d"}},"response":{"text":"success!"}}
]

通过 Postman 验证服务,测试 Get 请求

Moco 服务日志

09 十二月 2018 13:12:43 [nioEventLoopGroup-3-2] INFO  Request received:GET /7d/zuozewei HTTP/1.1
Host: 127.0.0.1:12306
User-Agent: PostmanRuntime/7.4.0
Accept: */*
Accept-Encoding: gzip, deflate
Cache-Control: no-cache
Content-Type: multipart/form-data; boundary=--------------------------445269276531904972620891
Postman-Token: f9deca3a-9b59-426c-ad48-00ebb4800321
X-Lantern-Version: 5.1.0
Content-Length: 009 十二月 2018 13:12:43 [nioEventLoopGroup-3-2] INFO  Response return:HTTP/1.1 200
Content-Length: 10
Content-Type: text/plain; charset=utf-8success!

10、约定请求 URI (endsWith)

JSON 脚本

[{"description":"这是一个请求endsWith URI","request":{"uri":{"endsWith":"/7d"}},"response":{"text":"success!"}}
]

通过 Postman 验证服务,测试 Get 请求

Moco 服务日志

09 十二月 2018 13:16:48 [nioEventLoopGroup-3-2] INFO  Request received:GET /zuozewei/7d HTTP/1.1
Host: 127.0.0.1:12306
User-Agent: PostmanRuntime/7.4.0
Accept: */*
Accept-Encoding: gzip, deflate
Cache-Control: no-cache
Content-Type: multipart/form-data; boundary=--------------------------516453569550782372688423
Postman-Token: 774378a6-5e57-4cc2-a015-f4b3bd2cb84d
X-Lantern-Version: 5.1.0
Content-Length: 009 十二月 2018 13:16:48 [nioEventLoopGroup-3-2] INFO  Response return:HTTP/1.1 200
Content-Length: 10
Content-Type: text/plain; charset=utf-8success!

11、约定请求 URI (Contain)

JSON 脚本

[{"description":"这是一个请求Contain URI","request":{"uri":{"contain":"/7d"}},"response":{"text":"success!"}}
]

通过 Postman 验证服务,测试 Get 请求

Moco 服务日志

09 十二月 2018 13:20:28 [nioEventLoopGroup-3-2] INFO  Request received:GET /zuozewei/7d/12345 HTTP/1.1
Host: 127.0.0.1:12306
User-Agent: PostmanRuntime/7.4.0
Accept: */*
Accept-Encoding: gzip, deflate
Cache-Control: no-cache
Content-Type: multipart/form-data; boundary=--------------------------030965700716204296542028
Postman-Token: 7615db1b-77e1-40f7-bdc3-e464c4e7269a
X-Lantern-Version: 5.1.0
Content-Length: 009 十二月 2018 13:20:28 [nioEventLoopGroup-3-2] INFO  Response return:HTTP/1.1 200
Content-Length: 10
Content-Type: text/plain; charset=utf-8success!

12、约定指定 Json 响应

JSON 脚本

[{"description":"这是一个指定Json响应的post请求","request":{"uri":"/7d","method":"post"},"response":{"json":{"name":"success","code":"1"}}}
]

通过 Postman 验证服务,测试 Post 请求

Moco 服务日志

09 十二月 2018 13:25:19 [nioEventLoopGroup-3-2] INFO  Request received:POST /7d HTTP/1.1
Host: 127.0.0.1:12306
User-Agent: PostmanRuntime/7.4.0
Content-Length: 0
Accept: */*
Accept-Encoding: gzip, deflate
Cache-Control: no-cache
Content-Type: multipart/form-data; boundary=--------------------------703341725381001692596870
Postman-Token: e5686919-85b9-44d0-8a73-61bf804b6377
X-Lantern-Version: 5.1.009 十二月 2018 13:25:19 [nioEventLoopGroup-3-2] INFO  Response return:HTTP/1.1 200
Content-Length: 29
Content-Type: application/json; charset=utf-8{"name":"success","code":"1"}

13、约定响应 Status

JSON 脚本

[{"description":"这是指定响应status的get请求","request":{"uri":"/7d","method":"get"},"response":{"status":200}}
]

通过 Postman 验证服务,测试 Get 请求

Moco 服务日志

09 十二月 2018 13:29:07 [nioEventLoopGroup-3-2] INFO  Request received:GET /7d HTTP/1.1
Host: 127.0.0.1:12306
User-Agent: PostmanRuntime/7.4.0
Accept: */*
Accept-Encoding: gzip, deflate
Cache-Control: no-cache
Content-Type: multipart/form-data; boundary=--------------------------465777039297587100709267
Postman-Token: 791fa21c-386f-4389-aaa9-ba06d9e53aff
X-Lantern-Version: 5.1.0
Content-Length: 009 十二月 2018 13:29:07 [nioEventLoopGroup-3-2] INFO  Response return:HTTP/1.1 200

14、约定响应 Headers

JSON 脚本

[{"description":"这是一个get请求","request":{"uri":"/7d","method":"get"},"response":{"headers":{"content-type":"application/json"}}}
]

通过 Postman 验证服务,测试 Get 请求

Moco 服务日志

09 十二月 2018 13:34:22 [nioEventLoopGroup-3-2] INFO  Request received:GET /7d HTTP/1.1
Host: 127.0.0.1:12306
User-Agent: PostmanRuntime/7.4.0
Accept: */*
Accept-Encoding: gzip, deflate
Cache-Control: no-cache
Content-Type: multipart/form-data; boundary=--------------------------774041889819140984857561
Postman-Token: 0a51f958-0338-4afa-8ff6-af45d61e12a7
X-Lantern-Version: 5.1.0
Content-Length: 009 十二月 2018 13:34:22 [nioEventLoopGroup-3-2] INFO  Response return:HTTP/1.1 200
content-type: application/json

15、约定响应 Cookies

JSON 脚本

[{"description":"这是一个响应Cookies的get请求","request":{"uri":"/7d","method":"get"},"response":{"cookies":{"login":"7dgroup"}}}
]

通过 Postman 验证服务,测试 Get 请求

Moco 服务日志

09 十二月 2018 13:39:00 [nioEventLoopGroup-3-2] INFO  Request received:GET /7d HTTP/1.1
Host: 127.0.0.1:12306
User-Agent: PostmanRuntime/7.4.0
Accept: */*
Accept-Encoding: gzip, deflate
Cache-Control: no-cache
Content-Type: multipart/form-data; boundary=--------------------------315176206881627055625168
Postman-Token: f6d1ae6b-c1c2-474a-827c-f02ed3f23482
X-Lantern-Version: 5.1.0
Content-Length: 009 十二月 2018 13:39:00 [nioEventLoopGroup-3-2] INFO  Response return:HTTP/1.1 200
Set-Cookie: login=7dgroup; Path=/

16、约定重定向 RedirectTo

JSON 脚本

[{"description":"这是一个重定向的get请求","request":{"uri":"/7d","method":"get"},"redirectTo":"http://blog.csdn.net/zuozewei"}
]

通过浏览器验证服务,测试 Get 请求
http://127.0.0.1:12306/7d

Moco 服务日志

09 十二月 2018 13:43:58 [nioEventLoopGroup-4-2] INFO  Request received:GET /7d HTTP/1.1
Host: 127.0.0.1:12306
User-Agent: PostmanRuntime/7.4.0
Accept: */*
Accept-Encoding: gzip, deflate
Cache-Control: no-cache
Content-Type: multipart/form-data; boundary=--------------------------167408129884853494096695
Cookie: login=7dgroup
Postman-Token: f83696d4-37ba-45b6-aff6-6f20982673ac
X-Lantern-Version: 5.1.0
Content-Length: 009 十二月 2018 13:43:58 [nioEventLoopGroup-4-2] INFO  Response return:HTTP/1.1 302
Location: http://blog.csdn.net/zuozewei

五、小结

Moco 的使用很简单,配置也很方便,目前更是提供了 http、rest、socket 服务。但是也仅仅是能 stub 接口,模拟出简单的场景。如果接收到请求后需要做一些处理,如需查询数据库、进行运算、或者一些复杂的操作,就无能为力了。所以是否选用 Moco,就取决于测试人员是否只是需要一个简单的模拟服务器。

本文源码:

  • https://github.com/zuozewei/blog-example/tree/master/Java-api-test/08-mock/moco-demo

走进Java接口测试之简单快速的Mock Server Moco相关推荐

  1. java接口fastjson_走进Java接口测试之fastjson指南

    来源:https://www.testwo.com 走进Java接口测试之fastjson指南s1.jpeg (147.9 KB, 下载次数: 0) 2020-10-5 23:59 上传 引言 在上文 ...

  2. java接口fastjson_JSON 走进Java接口测试之fastjson指南 _好机友

    新年加入啄木鸟公众号,好运滚滚來! 顾翔老师开发的bugreport2script开源了,希望大家多提建议.文件在https://github.com/xianggu625/bug2testscrip ...

  3. 走进Java接口测试之测试报告ExtentReport

    文章目录 一.引言 二.ExtentReports 简介 三.具体步骤 Step-1:添加 Maven 依赖包 Step-2:重写 ExtentTestNgFormatter 类 创建 MyExten ...

  4. java移动端接口测试_走进Java接口测试之测试框架TestNG数据驱动(入门篇)

    前言 我们在前面的文章中,和大家分享过接口自动化测试一些基本的实现方法,但是,你很快就会发现,如果在测试脚本中硬编码测试数据的话,测试脚本灵活性会非常低.而且,对于那些具有重复的请求,而只是测试入参不 ...

  5. 你不知道的接口测试之简单的开始

    当前的风气是,谈测试,必言"接口".其实接口并不神秘,当今的应用中无处不是"接口",从本篇开始让我来谈一些关于接口的浅薄认识. 一个简单的接口(demo.php ...

  6. php foreach 不等于_你不知道的接口测试之简单的开始

    当前的风气是,谈测试,必言"接口".其实接口并不神秘,当今的应用中无处不是"接口",从本篇开始让我来谈一些关于接口的浅薄认识. 一个简单的接口(demo.php ...

  7. PHP API接口数据简单快速的加密解密

    php7.0版本以上不支持mcrypt_encryp函数进行加密的代码,加密方式改为openssl_encrypt 用自己私人的服务器来测试吧,99买阿里云 openssl_系列支持php5.3以上版 ...

  8. java接口方法实现_Java接口的简单定义与实现方法示例

    本文实例讲述了Java接口的简单定义与实现方法.分享给大家供大家参考,具体如下: 1.接口是Java中最终要的概念,接口可以理解为一种特殊的类,里面全部是由全局常量和公共的抽象方法所组成. 2.接口的 ...

  9. java mockserver搭建_搭建Mock Server

    1.为什么要搭建mock-server? 为了更好的分工合作,让前端能在不依赖后端环境的情况下进行开发,其中一种手段就是为前端开发者提供一个 web 容器,这个本地环境就是 mock-server. ...

最新文章

  1. TPAMI 2022 | 国防科大等高校提出光场解耦机制,在超分辨与视差估计任务上取得优异性能...
  2. 【Visual Studio 扩展工具】使用ComponentOne中的GroupDefinition和SortDefinition属性保存和还原布局...
  3. AppSettings和ConnectionStrings的使用。
  4. Sql Injection 注入攻击
  5. leetcode 454. 四数相加 II c语言
  6. 用Hamcrest验证DateTime和日期
  7. Linux 如何通过某一台服务器调用执行多台远程服务器上的脚本,结果显示在本地?...
  8. 谷歌浏览器登录不了账号_谷歌浏览器使用分享(可谷歌账号登录)之谷歌账号登录...
  9. b+树的增删改查_考研计算机 | 如何理解m阶B树?
  10. mysql_udf_http(根据mysql表自动触发发送http请求)
  11. snmp v3 参数_SNMPv3 配置及snmpwalk命令信息获取
  12. 关于迪文屏T5L使用C51编程
  13. n维空间被m个n-1维的“刀”最多切出多少块
  14. android实现推箱子代码,android开发--推箱子小游戏(二)
  15. html页面整体隐藏,三种隐藏 HTML 元素的方式
  16. 新三板专家程晓明:四板将是推动区块链技术与资本市场结合试验田
  17. 虚拟化技术 — GuestOS Agent
  18. SpringBoot整合thymeleaf之模糊查询操作模块
  19. 《精通CFD工程仿真与案例实战---FLUENT GAMBIT ICEM CFD Tecplot(第2版)》—— 导读...
  20. 多彩标题文字PR字幕模板PR项目工程文件

热门文章

  1. 开源二维码QR CODE编码/解码(识别)库
  2. 【Hack The Box】windows练习-- Object
  3. CMWAP和CMNET 的区别与适用范围
  4. Python技能树及Markdown编辑器的测评
  5. python绘制玫瑰曲线_数学的有趣图形-玫瑰线
  6. Python实现决策树算法和朴素贝叶算法,并根据天气数据集预测是否出游
  7. GeoGebra 数列极限
  8. vue 中canvas 根据点画出圆滑的曲线
  9. python判断正数和负数教案_正数和负数优秀教案
  10. IT网管软件比较一览表