Mock框架moco學(xué)習(xí)筆記
一、Moco框架基本介紹
1. 什么是Moco
moco是一個(gè)簡(jiǎn)單搭建模擬服務(wù)器的程序庫(kù)/工具,是一個(gè)簡(jiǎn)單單間stub的框架,主要用于測(cè)試和集成。
Mock可以提供以下協(xié)議和類型的mock服務(wù):
- HTTP APIs
- Socket APIs
- REST API
2. Moco原理簡(jiǎn)介
- Moco會(huì)根據(jù)一些配置啟動(dòng)一個(gè)真正的HTTP服務(wù)(監(jiān)聽(tīng)本地的某個(gè)端口)。當(dāng)發(fā)起請(qǐng)求滿足一個(gè)條件時(shí),它就回復(fù)一個(gè)應(yīng)答。Moco的底層沒(méi)有依賴于像Servlet這樣的重型框架,而是基于一個(gè)叫Netty網(wǎng)絡(luò)應(yīng)用框架,這樣就繞過(guò)了復(fù)雜的應(yīng)用服務(wù)器。
- Moco本身支持API和獨(dú)立運(yùn)行兩種方式,通過(guò)使用API,測(cè)試開(kāi)發(fā)人員可以在JUnit、JBehave等測(cè)試框架中使用Moco。也可以世界通過(guò)Json腳本使用。
3. Moco的配置和運(yùn)行
java運(yùn)行環(huán)境+moco-runner.0.11.0-standalone.jar
jar包的下載地址:
https://repo1.maven.org/maven2/com/github/dreamhead/moco-runner/0.11.0/
可以下載jar包:

4. Moco啟動(dòng)以及第一個(gè)demo
啟動(dòng)命令:
java -jar ./moco-runner-0.11.0-standalone.jar 服務(wù)類型 -p 端口號(hào) -c json配置文件
例如:
java -jar ./moco-runner-0.11.0-standalone.jar http -p 8809 -c startup.json
如果要帶路徑:
java -jar "D:/moco-runner-0.11.0-standalone.jar" http -p 8809 -c "D:\startup.json"
說(shuō)明:
本地啟動(dòng)了一個(gè)http Server,其中監(jiān)聽(tīng)端口是8809,配置文件是 startup.json。用戶可以在本機(jī)發(fā)起一個(gè)請(qǐng)求,如 http://localhost:8809,json不支持注釋。description 字段中可以寫(xiě)注釋。
二、mock 的啟動(dòng)及第一個(gè)demo
1. 創(chuàng)建startup.json文件
[
{
"description": "這是我們的第一個(gè)mock例子",
"request": {
"uri": "/demo"
},
"response": {
"text": "Hello world"
}
}
]
2. 啟動(dòng)moco
java -jar ./moco-runner-0.11.0-standalone.jar http -p 8809 -c startup.json
3. 瀏覽器輸入本地網(wǎng)址127.0.0.1:8809/demo

這個(gè)是一個(gè)不帶參數(shù)的get方法
注意:修改json文件不用重新部署,服務(wù)熱部署,會(huì)自動(dòng)重啟
三、不帶參數(shù)的get方法:Method(GET)
[
{
"description": "模擬一個(gè)沒(méi)有參數(shù)的get請(qǐng)求",
"request": {
"uri": "/getdemo",
"method": "get"
},
"response": {
"text": "This is a request with no paramerters"
}
}
]
請(qǐng)求結(jié)果:

四、帶參數(shù)的get方法:queries
[
{
"description": "模擬一個(gè)有參數(shù)的get請(qǐng)求",
"request": {
"uri": "/getdemo",
"method": "get",
"queries": {
"name": "1",
"age": "2"
}
},
"response": {
"text": "This is a GET request with two paramerters"
}
}
]
請(qǐng)求結(jié)果:

五、不帶參數(shù)的post方法:Method(Post)
- json內(nèi)容:
[
{
"description": "模擬一個(gè)沒(méi)有參數(shù)的post請(qǐng)求",
"request": {
"uri": "/postdemo",
"method": "post"
},
"response": {
"text": "這是一個(gè)沒(méi)參數(shù)的post請(qǐng)求",
"headers":{
"Content-Type":"text/html;charset=gbk"
}
}
}
]
- 啟動(dòng):
java -jar ./moco-runner-0.11.0-standalone.jar http -p 8899 -c startupPost.json
- 使用jemeter做接口測(cè)試:
1)添加線程組
2)添加Sampler-HTTP請(qǐng)求
3)添加監(jiān)聽(tīng)器-查看結(jié)果樹(shù)
4)HTTP請(qǐng)求添加參數(shù)
![]()
5)查看結(jié)果:
![]()
![]()
六、帶參數(shù)的post方法:Forms
- json內(nèi)容:
[
{
"description": "模擬一個(gè)有參數(shù)的post請(qǐng)求",
"request": {
"uri": "/with/params/postdemo",
"method": "post",
"forms": {
"name": "1",
"age": "2"
}
},
"response": {
"text": "這是一個(gè)有參數(shù)的post請(qǐng)求",
"headers":{
"Content-Type":"text/html;charset=gbk"
}
}
}
]
- 啟動(dòng):
java -jar ./moco-runner-0.11.0-standalone.jar http -p 8899 -c startupPost.json
- 使用jemeter做接口測(cè)試:
1)添加線程組
2)添加Sampler-HTTP請(qǐng)求和參數(shù)
![]()
3)添加監(jiān)聽(tīng)器-查看結(jié)果樹(shù)
![]()
![]()
七、Moco框架中如何加入Cookies
1. 請(qǐng)求中帶cookies信息的get請(qǐng)求
- json內(nèi)容:
[
{
"description":"這是一個(gè)request請(qǐng)求中帶cookies信息的get請(qǐng)求",
"request":{
"uri":"/get/with/cookies",
"method":"get",
"cookies":{
"login":"true"
}
},
"response":{
"text":"返回這是一個(gè)request請(qǐng)求中帶cookies信息的get請(qǐng)求"
}
}
]
- 啟動(dòng)服務(wù):
java -jar ./moco-runner-0.11.0-standalone.jar http -p 8899 -c startupPost.json
- 使用jmeter做接口測(cè)試:
1)添加線程組
2)添加Sampler-HTTP請(qǐng)求和參數(shù)
![]()
3)添加HTTP cookie管理器:
![]()
4)添加監(jiān)聽(tīng)器-查看結(jié)果樹(shù)
![]()
2. 請(qǐng)求中帶cookies的post請(qǐng)求
- json內(nèi)容:
[
{
"description":"這是一個(gè)帶cookies信息的post請(qǐng)求",
"request":{
"uri":"/post/with/cookies",
"method":"post",
"cookies":{
"login":"true"
},
"json":{
"name":"huhansan",
"age":"18"
}
},
"response":{
"status":200,
"json":{
"huhansan":"success",
"status":"1"
},
"headers":{
"Content-Type":"text/html;charset=gbk"
}
}
}
]
- 使用jmeter做接口測(cè)試:
1)添加線程組
2)添加Sampler-HTTP請(qǐng)求和參數(shù)
![]()
3)添加HTTP cookie管理器:同上
4)添加監(jiān)聽(tīng)器-查看結(jié)果樹(shù)
![]()
![]()
![]()
3. response返回中帶有cookies信息的get請(qǐng)求
- json內(nèi)容:
[
{
"description":"模擬response返回中帶有cookies信息的get請(qǐng)求",
"request":{
"uri":"/resopnse/with/cookies",
"method":"get"
},
"response":{
"cookies":{
"login":"true",
"token":"1234567890"
},
"json":{
"name":"zhoujielun",
"age":"38"
}
}
}
]
- 使用jmeter做接口測(cè)試:
1)添加線程組
2)添加Sampler-HTTP請(qǐng)求和參數(shù)
![]()
3)添加監(jiān)聽(tīng)器-查看結(jié)果樹(shù)
![]()
![]()
4. 帶header信息的post請(qǐng)求
- json內(nèi)容:
[
{
"description":"這是一個(gè)帶header信息的post請(qǐng)求",
"request":{
"uri":"/post/with/headers",
"method":"post",
"headers":{
"content-type":"application/json"
},
"json":{
"name":"huqiqi",
"sex":"women"
}
},
"response":{
"json":{
"huqiqi":"success",
"status":1
}
}
]
- 使用jmeter做接口測(cè)試:
1)添加線程組
2)添加Sampler-HTTP請(qǐng)求和參數(shù)
![]()
3)jemeter添加HTTP信息頭管理器:
![]()
![]()
4)添加監(jiān)聽(tīng)器-查看結(jié)果樹(shù)
![]()
![]()
![]()
八、Moco框架中如何進(jìn)行重定向
1. 有重定向的接口(redirectTo)
- json內(nèi)容:
[
{
"description":"模擬重定向接口",
"request":{
"uri":"/redirectto"
},
"redirectTo":"/redirectedpath"
},
{
"description":"模擬被重定向的接口",
"request":{
"uri":"/redirectedpath"
},
"response":{
"text":"返回重定向結(jié)果",
"headers":{
"Content-Type":"text/html;charset=gbk"
}
}
}
]
- 使用jmeter做接口測(cè)試:
1)添加線程組
2)添加Sampler-HTTP請(qǐng)求和參數(shù):HTTP請(qǐng)求勾選Follow Redirects(跟隨重定向):
![]()
3)添加監(jiān)聽(tīng)器-查看結(jié)果樹(shù)
![]()
![]()
九、Moco框架響應(yīng)
1. 約定以指定json作為響應(yīng)
[
{
"request":{
"uri":"/responseJson"
},
"response":{
"json":{
"username":"mocor"
}
}
}
]
2. 約定Status:模擬各種狀態(tài)碼以達(dá)到我們測(cè)試需要的結(jié)果
[
{
"request":{
"uri":"/responseStatus"
},
"response":{
"status":200
}
}
]
3. 約定響應(yīng)Headers
[
{
"request":{
"uri":"/responseHeaders"
},
"response":{
"headers":{
"content-type":"application/json"
}
}
}
]
4. 約定響應(yīng)Cookies
[
{
"request":{
"uri":"/responseCookies"
},
"response":{
"cookies":{
"username":"chenlei"
}
}
}
]

























浙公網(wǎng)安備 33010602011771號(hào)