衆所周知Mock是爲了解決依賴接口未開發完成或者調用第三方接口的情況,進行模擬返回數據而存在,測試中具體使用場景需要在真實使用後進行補充------未完待續~
下面是自己整理的關於Mock的使用之一:
1、首先依賴提供的jar包和git命令
jar包下載路徑:https://repo1.maven.org/maven2/com/github/dreamhead/moco-runner/0.11.0
java -jar ./moco-runner-0.11.0-standalone.jar http -p 8888 -c startup1.json
瀏覽器訪問路徑:localhost:8888/指定方法名
2、模擬get請求的兩種方式(無參和傳參)
//無參
[
{
"description":"這是第一個demo",
"request":{
"uri":"/demo",
"method":"get"
},
"response":{
"text":"這是第一個mock框架的demo",
//解決亂碼問題
"headers":{
"Content-Type":"text/html;charset=gbk"
}
}
}
]
//有參
[
{
"description":"模擬一個帶參數的get請求",
"request":{
"uri":"/getwithparam",
"method":"get",
"queries":{
"name": "huhansan",
"age": "18"
}
},
"response":{
"text": "我胡漢三又回來啦",
"headers": {
"Content-Type":"text/html;charset=gbk"
}
}
}
]
3、模擬post請求的兩種方式(無參、json格式和非json格式)
//無參post請求
[
{
"description": "模擬一個post請求",
"request": {
"uri": "/postdemo",
"method": "post"
},
"response": {
"text": "這是我的第一個mock_post請求",
"headers": {
"Content-Type":"text/html;charset=gbk"
}
}
}
]
//json格式post請求
[
{
"description": "json格式post請求",
"request": {
"uri": "/postdemo",
"method": "post",
"json":{
"name":"huhansan",
"age":""18
}
},
"response": {
"text": "json格式post請求",
"headers": {
"Content-Type":"text/html;charset=gbk"
}
}
}
]
//非json格式post請求
[
{
"description": "非json格式post請求",
"request": {
"uri": "/postdemo",
"method": "post",
"forms":{
"name":"lisi",
"age":"20"
}
},
"response": {
"text": "非json格式post請求",
"headers": {
"Content-Type":"text/html;charset=gbk"
}
}
}
]
4、帶cookie信息的get請求
[
{
"description":"帶cookie信息的get請求",
"request":{
"uri":"/get/cookie",
"method":"get",
"cookies":{
"login":"true"
}
},
"response":{
"text":"cookie信息驗證成功",
"headers":{
"Content-Type":"text/html;charset=gbk"
}
}
}
]
5、帶cookie請求的post請求且請求數據指定爲json格式
[
{
"description":"攜帶cookie信息的post請求",
"request":{
"uri":"/post/cookies",
"method":"get",
"cookies":{
"login":"true"
},
"queries":{
"name":"zhaoliu",
"sex":"woman"
},
"headers":{
"content-type":"application/json"
}
},
"response":{
"status":201,
"json":{
"name":"zhaoliu",
"sex":"woman"
}
}
}
]
6、重定向
[
{
"description":"重定向到自己的網頁上",
"request":{
"uri":"/redirect/topath"
},
"redirectTo":"/redirect/new"
},
{
"description":"這是被重定向的請求",
"request":{
"uri":"/redirect/new"
},
"response":{
"text":"重定向成功了"
}
}
]