Moco框架介绍和使用

 一、Moco框架介绍

mock测试:mock测试就是在测试过程中,对于某些不容易构造或者不容易获取的对象,用一个虚拟的对象来创建以便测试的测试方法。一般可分为模拟调用方和被调用方2种方式。

mock用来模拟接口,这里mock用的是moco框架,moco框架是github上的一个开源项目,可模拟http,https,Socket协议。moco有几种使用方法,这里介绍的是standolone用法。

二、Moco框架使用

1、从github上下载最新版本的moco-runner-1.2.0-standalone.jar包

下载地址:https://repo1.maven.org/maven2/com/github/dreamhead/moco-runner/

选择最新的或者其他版本,我这里选择了当前最新的1.2.0版本

 

 2、新建一个文件夹保存下载的moco-runner-1.2.0-standalone.jar文件

 新建一个json文件,如demo.json:

内容如下:

[
  {
    "response" :
      {
        "text" : "This is moco demo!"
      }
  }
]

启用命令行(windows键+R键:输入cmd回车),进入json文件夹所在目录

在dos窗口下可以依次执行:

 切换到json文件路径下,执行如下命令启动mock服务

java -jar moco-runner-1.2.0-standalone.jar http -p 8008 -c demo.json

moco-runner-1.2.0-standalone.jar   下载的jar包文件名
8008  访问的端口号,可以随意定义
demo.json  新建的json文件名

如图所示说明启动成功了

启动成功后浏览器访问地址即可查看效果:

http://localhost:8008/demo

 三、Moco其他请求方法的使用介绍

1、get请求

1.1 不带参数的get请求
复制代码
[
  {
    "description":"不带参数的get请求",
    "request":{
      "uri":"/withGetDemo",
      "method":"get"
    },
    "response":{
      "text":"这是不带参数的get请求"
    }
  }
]
复制代码

命令:java -jar moco-runner-1.2.0-standalone.jar http -p 8008 -c withGetDemo.json

访问地址:http://localhost:8008/withGetDemo

结果如图所示:

1.2 带参数的get请求
复制代码
[
  {
    "description":"带参数的get请求,p1,p2分别的参数1,参数2,名称可随便起,个数也可随便加",
    "request":{
      "uri":"/wihtGetDemobyParam",
      "method":"get",
      "queries":{
        "p1":"hh",
        "p2":"good"
      }
    },
    "response":{
      "text":"这是带参数的get请求"
    }
  }
]
复制代码

命令:java -jar moco-runner-1.2.0-standalone.jar http -p 8008 -c wihtGetDemobyParam.json

访问地址:http://localhost:8008/wihtGetDemobyParam

请求的完整地址:http://localhost:8008/wihtGetDemobyParam?p1=hh&p2=good

结果如图所示:

也可以用postman模拟请求:

2、post请求

2.1 不带参数的post请求
复制代码
[
  {
    "description":"post 请求",
    "request":{
      "uri":"/postDemo",
      "method":"post"
    },
    "response":{
      "text":"This is post request"
    }
  }
]
复制代码

命令:java -jar moco-runner-1.2.0-standalone.jar http -p 8008 -c postDemo.json

访问地址:http://localhost:8008/postDemo

结果如图所示:

2.2 带参数的post请求
复制代码
[
  {
    "description":"带参数的post请求",
    "request":{
      "uri":"/postDemoWithParam",
      "method":"post",
      "forms":{
        "param1":"one",
        "param2":"two"
      }
    },
    "response":{
      "text":"this is post request with param"
    }
  }
]
复制代码

命令:java -jar moco-runner-1.2.0-standalone.jar http -p 8008 -c postDemoWithParam.json

访问地址:http://localhost:8008/postDemoWithParam

结果如图所示:

 

 

 2.3 请求参数为josn格式的post请求
复制代码
[
  {
    "description":"带json格式参数的post请求",
    "request":{
      "uri":"/postDemoWithJson",
      "method":"post",
      "json":{
        "param1":"one",
        "param2":"two"
      }
    },
    "response":{
      "status":"200",
      "json":{
        "name":"success",
        "status":"1"
      }
    }
  }
]
复制代码

命令:java -jar moco-runner-1.2.0-standalone.jar http -p 8008 -c postDemoWithJson.json

访问地址:http://localhost:8008/postDemoWithJson

结果如图所示:

演示使用的postman模拟post请求,body里设置raw格式为JSON

 3、请求重定向

复制代码
[
  {
    "description":"重定向到百度",
    "request":{
      "uri":"/redirect"
    },
    "redirectTo":"http://www.baidu.com"
  },
  {
    "description":"重定向到自己的网页上",
    "request":{
      "uri":"/redirect/topath"
    },
    "redirectTo":"/redirect/new"
  },
  {
    "description":"这是被重定向到的请求",
    "request":{
      "uri":"/redirect/new"
    },
    "response":{
      "text":"重定向成功"
    }
  }
]
复制代码

命令:java -jar moco-runner-1.2.0-standalone.jar http -p 8008 -c redirect.json

访问地址:

http://localhost:8008/redirect

 

 重定向到百度

http://localhost:8008/redirect/topath

结果如图所示:

其他:

对应的附件文件如图所示:

 

 

 

 

 

 

参考文章:https://blog.csdn.net/weixin_43890267/article/details/88203199

github地址:https://github.com/dreamhead/moco

 

posted @   李家新生代农民工  阅读(514)  评论(0编辑  收藏  举报
编辑推荐:
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
阅读排行:
· 周边上新:园子的第一款马克杯温暖上架
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?
· 使用C#创建一个MCP客户端
点击右上角即可分享
微信分享提示