wiremock 模拟数据

1、wire mock 概述

很强大好用的 mock 。可以让客户端和服务端进行解耦开发。模拟数据的很彻底不用写php数组直接一个json就搞定了。

2、环境搭建

很尴尬win的没搭成功,换成mac 就直接成功了。尴尬
点击查阅官网

对Running as a Standalone Process 模式配置进行简单的记录下。

  1. 下载wiremock-standalone jar包:downloaded the standalone JAR

  2. 把jar包放到指定目录,切换到改目录,执行

java -jar wiremock-standalone-2.5.1.jar

//如果要改port 可以加--port xxx (默认是8080)
//--port: Set the HTTP port number e.g. --port 9999

对应生成2个文件夹:mappings 与__files.
其中mappings 放json格式的请求映射表, __files文件放文件,可以提供下载,也可以给mappings中的json响应用。

  1. 在mappings文件夹里新建存放json的文件,如demo.json.
{
    "request": {
        "method": "GET",
        "url": "/api/mytest"
    },
    "response": {
        "status": 200,
        "body": "More content\n"
    }
}

4、浏览器访问:http://localhost:8080/api/mytest 或者命令访问:curl http://localhost:8080/api/mytest。返回:More content 就成功了

5、body里不能直接写json,可以把返回的数据写成一个文件放在__files里加载

{
  "request": {
    "method": "GET",
    "url": "/api"
  },
  "response": {
    "status": 200,
    "bodyFileName":"aaa.json"
  }
}

对应就是__files文件夹下的 aaa.json 文件

猜你喜欢

转载自blog.csdn.net/lckj686/article/details/57323523