Moco框架的介绍与使用

一、moco框架基本介绍

mock用来模拟接口,这里mock用的是moco框架,moco框架是github上的一个开源项目,可模拟http,https,Socket协议。

二、moco框架的入门使用

从github上下载jar包,https://repo1.maven.org/maven2/com/github/dreamhead/moco-runner/0.11.1/,选择最大的
在这里插入图片描述
在需要的进行测试的项目中创建一个project,将下载下来的jar包直接拷进文件夹

建立一个json文件(图例中json文件名为startup1),输入以下代码,保存

以下代码可以复制

   [
      {
        "description":"这是第一个moco例子",
        "request":{
          "uri":"/demo"
        },
        "response":{
          "text":"这是一个moco框架demo"
        }
      }
    ]

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

图例中的进入方式

然后输入以下命令,运行
java -jar -Dfile.encoding=UTF-8 ./moco-runner-0.10.0-standalone.jar http -p 8080 -c
startup1.json
-Dfile.encoding=UTF-8:字符集
-p 8080:访问的端口号(8080)
-c startup1.json:访问的字符集

例如:localhost:8080/demo

moco 请求应用
get请求
不带参数的get请求

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

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

带参数的get参数请求

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

访问地址::http://localhost:8080/wihtGetDemobyParam?p1=hh&p2=good

Post请求
不带参数的post请求

[
  {
    "description":"post 请求",
    "request":{
      "uri":"/postDemo",
      "method":"post"
    },
    "response":{
      "text":"This is post request"
    }
  }
]

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

带参数的post请求

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

访问地址:http://localhost:8080/postDemoWithParam
post参数为json格式(返回值也是json格式)

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

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

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值