环境搭建
下载安装:依赖JDK环境,执行电脑安装好JDK,建议安装JDK1.8版本
Server-Moco下载地址:https://repo1.maven.org/maven2/com/github/dreamhead/moco-runner,选择相应版本,本次下载最新版1.3.0,下载下来就是moco-runner-1.3.0-standalone.jar包

Mock单个请求
  • 创建一个json 文件,名为 login.json,内容如下:

[
  {
    ”description”: “登录”,
    ”request”: {
      ”uri”: “/login”
        },
    ”response”: {
      ”text”:”登录成功”
        }

  }
]

  • 启动 Mock 服务

java -jar moco-runner-1.3.0-standalone.jar http -p 8082 -c login.json

 

 

 在Postman 发送请求:http://localhost:8082/login(这就mock好啦)

 

 

 

Mock多个请求
  • 在创建一个json 文件,名为 home.json,内容如下:

[
  {
    ”description”: “首页”,
    ”request”: {
      ”uri”: “/home”
        },
      ”response”: {
        ”text”:”进入首页”
        }

  }
]

  • 创建一个请求管理配置文件,名为 config.json,内容如下:

[
  {“include”: “login.json”},
  {“include”: “home.json”}
]

  • 启动 Mock 服务
  • java -jar moco-runner-1.3.0-standalone.jar http -p 8082 -g config.json

 

 

 在Postman 发送请求:http://localhost:8082/login 和 http://localhost:8082/home

 

 

 常见参数配置

  • 请求方式,通过method参数定义

[
  {
    ”description”: “登录”,
    ”request”: {
      ”uri”: “/login”,
    ”method”: “post”
        },
    ”response”: {
    ”text”: “登录成功”
          }
  }
]

  • 请求参数,通过queries参数定义

[
  {
    ”description”: “登录”,
    ”request”: {
      ”uri”: “/login”,
      “method”: “post”,
      ”queries”: {
        ”username”: “king”,
        ”pwd”: 123456
            }
        },
    ”response”: {
      ”text”: “登录成功”
          }
  }
]

  • 请求头,通过headers参数定义

[
  {
    ”description”: “登录”,
    ”request”: {
      ”uri”: “/login”,
      ”method”: “post”,
      ”headers”: {
        ”appId”: “4275CCE0-CE16-4BCC-9AF5-31CA133AC9AD”
            }
          },
    ”response”: {
      ”text”: “登录成功”
          }
  }
]

  • 表单请求体,通过forms参数定义

[
  {
    ”description”: “登录”,
    ”request”: {
      ”uri”: “/login”,
      ”method”: “post”,
      ”forms”: {
        ”username”: “king”,
        ”password”: 123456
            }
        },
    ”response”: {
      ”text”: “登录成功”
          }
  }
]

  • JSON请求体,通过json参数定义

[
  {
    ”description”: “登录”,
    ”request”: {
      ”uri”: “/login”,
      ”method”: “post”,
      ”headers”: {
        ”Content-Type”: “application/json”
            },
      ”json”: {
        ”username”: “king”,
        ”password”: 123456
            }
        },
    ”response”: {
      ”text”: “登录成功”
          }
  }
]

  • HTTP响应状态码,通过status参数定义

[
  {
    ”description”: “登录”,
    ”request”: {
    ”uri”: “/login”,
    ”method”: “post”
        },
    ”response”: {
      ”status”: 500,
      ”text”: “服务器出现异常!”
          }
  }
]

  • JSON响应数据,通过json参数定义

[
  {
    ”description”: “登录”,
    ”request”: {
      ”uri”: “/login”,
      ”method”: “post”
          },
    ”response”: {
      ”headers”: {
        ”Content-Type”: “application/json;charset=UTF-8”
            },
      ”json”: {
        ”code”: “0”,
        ”msg”: “操作成功”,
        ”data”: {
          ”userID”: 3175896456,
          ”token”: “17e6ad42cff592-0dfc3f6fa02aee-c343365-1fa400-17e6ad42d00891”
            }
          }
        }
  }
]

 

原文地址:http://www.cnblogs.com/ailie/p/16928397.html

1. 本站所有资源来源于用户上传和网络,如有侵权请邮件联系站长! 2. 分享目的仅供大家学习和交流,请务用于商业用途! 3. 如果你也有好源码或者教程,可以到用户中心发布,分享有积分奖励和额外收入! 4. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解! 5. 如有链接无法下载、失效或广告,请联系管理员处理! 6. 本站资源售价只是赞助,收取费用仅维持本站的日常运营所需! 7. 如遇到加密压缩包,默认解压密码为"gltf",如遇到无法解压的请联系管理员! 8. 因为资源和程序源码均为可复制品,所以不支持任何理由的退款兑现,请斟酌后支付下载 声明:如果标题没有注明"已测试"或者"测试可用"等字样的资源源码均未经过站长测试.特别注意没有标注的源码不保证任何可用性