【Java自动化测试】-Mock操作详解
一、moco框架
下载地址:https://repo1.maven.org/maven2/com/github/dreamhead/moco-runner/1.3.0/
moco执行:
java -jar ./moco-runner-1.3.0-standalone.jar http -p 8888 -c ./startup1.json
moco:json脚本编写
添加:description、request、response
[ { "description": "这是我们的第一个mokc例子", "request": { "uri": "/demo" }, "response": { "text": "这是moco框架说明" } } ]
moco模拟不带参数get请求
增加:request-method
[ { "description": "模拟一个没有参数的get请求", "request": { "uri":"/getdemo", "method": "get" }, "response": { "text": "get请求的输出结果" } } ]
moco模拟带参数get请求
增加request-queries
[ { "description": "模拟一个没有参数的get请求", "request": { "uri":"/getdemo", "method": "get" }, "response": { "text": "get请求的输出结果" } }, { "description": "模拟一个带参数的get请求", "request": { "uri": "/getparam", "method": "get", "queries": { "name": "zhangsan", "age": "18" } }, "response": { "text": "这是带参数的get输出" } } ]
moco模拟不带参数的post请求
[ { "description": "模拟一个post请求", "request": { "uri": "/postdemo", "method": "post" }, "response": { "text": "这是post的输出" } } ]
moco模拟带参数的post请求
增加request-forms
[ { "description": "模拟一个带参数的post请求", "request": { "uri": "/postparam", "method": "post", "forms": { "name": "zhangsan", "age": "18" } }, "response": { "text": "这是带参数post的输出" } } ]
带cookie的get请求
添加:request-cookies
[ { "description": "模拟带cookie的get请求", "request": { "uri": "/getcookie", "method": "get", "cookies": { "login": "true" } }, "response": { "text": "带cookie的get请求输出" } ]
带cookie的post请求
添加:request-cookies
请求json格式及返回格式为json:request-json,response-json
{ "description":"模拟带cookie的post请求", "request":{ "uri":"/post/cookie", "method": "post", "cookies":{ "login":"true" }, "json":{ "name":"hanxin", "age":"20" } }, "response": { "status": 200, "json": { "hanxin": "success", "status": "1" } } }
带header的请求
添加:request-header
{ "description": "", "request": { "uri": "/post/header", "method": "post", "headers": { "content-type": "application/json" }, "json": { "name": "han", "sex": "woman" } }, "response": { "status": 200, "json": { "data": "header---success", "status": "1" } } }
模拟重定向
使用redirectTo
{ "description": "重定向到百度", "request": { "uri": "/redirect" }, "redirectTo": "http://www.baidu.com" }
本文来自博客园,作者:橘子偏爱橙子,转载请注明原文链接:https://www.cnblogs.com/xfbk/p/17019974.html