Mock工具之Moco使用

发布时间 2023-10-09 14:29:05作者: 弩哥++

一、什么是Mock

mock英文单词有愚弄、嘲笑、模拟的意思,这里主要是模拟的意思

二、什么是Moco

  • 开源的、基于java开发的一个mock框架
  • 支持http、https、socket等协议

三、Mock的特点

  • 只需要简单的配置request、response等即可满足要求

  • 支持在request 中设置headers、cookies等

  • 支持GET、POST、PUT、DELETE等请求方法

  • 无需环境配置,有Java环境即可

  • 修改配置文件后,立刻生效

  • 对可能用到的数据格式都支持,如json、text、xml、file等。

四、什么场景会用到

  • 模拟第三方接口的返回
  • 后端接口还没有开发完毕,前端想要进行联调
  • 接口测试过程中,可能某些接口依赖有问题,也可以使用mock

五、Moco的原理

  • 根据json配置文件,启动一个http的服务,监听指定的端口

六、环境准备

七、环境搭建

  • 安装jdk,配置环境变量
  • 把moco-runner-1.5.0-standalone.jar 和配置文件如moco.json放同一目录
    image

八、Moco配置文件

moco配置文件格式必须是json格式。配置文件是个数组,也就是说,可以在一个文件中配置多个接口的请求和响应

配置文件常用字段
image

九、启动

# http 指定协议
# -p 指定端口
# -c 指定配置文件
java -jar moco-runner-1.5.0-standalone.jar http -p 8088 -c moco.json

十、示例

  • demo1
[
  {
    "description":"这是一个moco例子",
    "request":{
        "uri":"/demo"
    },
    "response":
    {
        "text":"Hello,Moco",
        "status": "200"
    }
  }
]
  • get请求,不带参数
[
    {
    "description":"这是一个get请求,不带参数",
    "request":{
        "uri":"/goods",
        "method": "get"
    },
    "response":
    {
        "headers": {
            "Content-Type": "text/plain; charset=GBK"
        },
        "text":"这是一个GET请求,不带参数",
        "status": "200"
    }
  }
]
  • get请求,带参数
[
	{
    "description":"这是一个get请求,带参数",
    "request":{
        "uri":"/GetWithQuery",
        "method": "get",
        "queries": {
            "username": "john",
            "password": "123123"
        }
    },
    "response":
    {
        "headers": {
            "Content-Type": "text/plain; charset=GBK"
        },
        "text":"这是一个GET请求,带参数",
        "status": "200"
    }
  },
]
  • post请求,带参数
[
	{
    "description":"这是一个post请求,带参数",
    "request":{
        "uri":"/PostWithQuery",
        "method": "post",
        "queries": {
            "username": "john",
            "password": "123123"
        }
    },
    "response":
    {
        "text":"这是一个POST请求,带参数",
        "status": "200",
        "headers": {
            "Content-Type": "text/plain; charset=GBK"
        }
    }
  }
]
  • post请求,不带参数
[
	{
        "description": "这是一个post请求,不带参数",
        "request": {
            "uri": "/WithPost",
            "method": "post"
        },
        "response": {
            "text": "这是一个post请求,不带参数",
            "status": "200",
            "headers": {
                "Content-Type": "text/plain; charset=GBK"
            }
        }

    }
]
  • post请求,带cookie参数
[
	{
        "description": "这是一个post请求,带cookie参数",
        "request": {
            "uri": "/PostWithCookie",
            "method": "post",
            "cookies": {
                "token": "123123"
            }
        },
        "response": {
            "text": "这是一个post请求,带cookie参数",
            "status": "200",
            "headers": {
                "Content-Type": "text/plain; charset=GBK"
            }
        }

    }
]
  • post请求,带forms参数
[
	{
        "description": "这是一个post请求,带forms参数",
        "request": {
            "uri": "/PostWithForm",
            "method": "post",
            "forms": {
                "username": "jake",
                "password": "123123"
            }
        },
        "response": {
            "text": "这是一个post请求,带forms参数",
            "status": "200",
            "headers": {
                "Content-Type": "text/plain; charset=GBK"
            }
        }

    }
]
  • 重定向
[
	{
        "description": "这是一个重定向",
        "request": {
            "uri": "/redirect",
            "method": "get"

        },
        "redirectTo": "http://www.baidu.com"

    }
]

十一、多配置文件模式

为了模拟多个接口,以及方便管理这些接口,moco-runner增加了配置模式,具体如下:

  • 首先,创建多个接口文件,比如:login.json,index.json
  • 然后,在当前文件夹下创建配置文件,config.json,用于管理接口文件login.json,index.json
  • 最后,用参数-g启动服务

接口文件login.json

[
	{
    "description":"这是登录接口",
    "request":{
        "uri":"/login",
        "method": "post",
        "queries": {
            "username": "xtank",
            "password": "123123"
        }
    },
    "response":
    {
        "status": "200",
        "token": "123456",
        "text": "登录成功!"
    }
  }
]

接口文件index.json

[
	{
    "description":"这是首页接口",
    "request":{
        "uri":"/index",
        "method": "get",
        "cookies": {
                "token": "123456"
            }
    },
    "response":
    {
        "status": "200",
        "token": "123456",
        "text": "访问首页!"
    }
  }
]

配置文件config.json

[
    {
    	"include":"login.json"
    },
    {
    	"include":"index.json"
    }
]

十二、中文乱码问题

加上参数 -Dfile.encoding=utf-8
java -Dfile.encoding=utf-8 -jar moco-runner-1.5.0-standalone.jar http -p 8088 -c moco.json

十三、参考

示例部分参考了 :https://www.cnblogs.com/xyztank/articles/12363534.html