Mock接口框架的应用实战

版权声明:作者已开启版权声明,如转载请注明转载地址。 https://blog.csdn.net/qq_34829447/article/details/83247142

一.Mock平台功能介绍

  • Mock平台可以帮助前端人员进行接口的模拟,本文介绍Moco框架

二.Moco框架基本介绍

三.Moco的启动及第一个demo

  • 创建文件夹MocoDemo

  • 将下载的moco-runner-standalone.jar放到MocoDemo文件夹下

  • jar的启动命令

    • java -jar ./moco-runner-0.11.0-standalone.jar http -p 8888 -c json配置文件路径
      • http表示协议
      • -p表示端口号
      • -c表示json配置文件
  • 编写startup1.json文件

    [
      {
        "description":"这是我们的第一个mock例子",
        "request":{
          "uri":"/demo"
        },
        "response":{
          "text":"第一个moco框架demo"
        }
      }
    ]
    
    • 其中request表示请求内容uri表示请求路径,如果没写请求方式则默认get
    • 其中response表示响应内容
  • 运行jar:java -jar ./moco-runner-0.11.0-standalone.jar http -p 8888 -c startup1.json后,访问localhost:8888页面上会打印第一个moco框架demo

四.Moco框架的http协议get方法Moco实现

  • 添加startupget.json文件

    [
      {
        "description":"模拟一个没有参数的get请求",
        "request":{
          "uri":"/getdemo",
          "method":"get"
        },
        "response":{
          "text":"这是一个没有参数的get请求"
        }
      },
      {
        "description":"模拟一个带参数的get请求",
        "request":{
          "uri":"/getwithparam",
          "method":"get",
          "queries":{
            "name":"huhansan",
            "age":"18"
          }
        },
        "response":{
          "text":"我胡汉三又回来了!!!"
        }
      }
    ]
    
    • 第一个对象是带参数的get请求;第二个对象时不带参数的get请求
    • 第一个请求的url是/getdemo,返回:这是一个没有参数的get请求;第二个请求的url是/getwithparam,必须携带name是huhansan、age是18的请求参数,返回:我胡汉三又回来了!!!
  • 运行jar:java -jar moco-runner-0.11.0-standalone.jar http -p 8888 -c startupget.json后,访问localhost:8888/getwithparam访问失败(因为没携带参数);访问localhost:8888/getwithparam?name=huhansan&age=18访问成功,页面显示:我胡汉三又回来了

未完待续…

猜你喜欢

转载自blog.csdn.net/qq_34829447/article/details/83247142