zoukankan      html  css  js  c++  java
  • mock服务

    mock测试

    在接口测试过程中,对于某些不容易构造或者不容易获取的对象,我们常常会用一个虚拟的对象代替以便测试。

    mock 使用

    https://github.com/dreamhead/moco
    One is API, which you can use in your unit test.
    The other is that run Moco as standalone.
    Currently, you put all your configuration in JSON file.

    原理

    根据配置,启动真正的HTTP服务(监听本地某端口)。发起请求满足一条件时,就会收到应答。
    基于Netty的的网络应用框架编写。

    mock 对象 单测

    moco 启动

    java -jar ./moco-runner-0.12.0-standalone.jar http -p 8888 -c demo.json

    demo
    
    [
      {
        "description": "My mock demo",
        "request": {
          "uri":"/"
        },
        "response": {
          "text": "Moco demo is running"
        }
      }
    ]
    

    get

    
    热部署:服务不用停止,就可以完成升级。
    [
      {
        "description":"模拟一个没有参数的get 请求",
        "request":{
          "uri": "/getdemo",
          "method": "get"
        },
        "response": {
          "text": "这是一个没有参数的get请求"
        }
      },
      {
        "description": "模拟一个带参数的get请求",
        "request": {
          "uri": "/getwithparam",
          "method": "get",
          "queries": {
            "name": "zhangsan",
            "age": "18"
          }
        },
        "response": {
          "text": "hello zhangsan!!!!"
        }
      }
    ]
    
    

    post

    [
      {
        "description": "模拟post请求",
        "request": {
          "uri": "/postdemo",
          "method": "post"
        },
        "response": {
          "text": "这是mock的post请求"
        }
      },
      {
        "description": "这是带参数带post请求",
        "request": {
          "uri": "/postwithparam",
          "method": "post",
          "forms": { 
            "name": "zhangsan",
            "age": "18"
          }
        },
        "response": {
          "text": "带参数带post请求"
        }
      }
    ]
    

    cookies

    [
      {
        "description": "这是一个带cookie信息的get请求",
        "request": {
          "uri": "/get/with/cookie",
          "method": "get",
          "cookies": {
            "login": "true"
          }
        },
        "response": {
          "text": "这是一个带cookie信息的get请求"
        }
      },
      {
        "description": "这是一个带cookie信息的post请求",
        "request": {
          "uri": "/post/with/cookie",
          "method": "post",
          "cookies": {
            "login": "true"
          },
          "json": {
            "name": "zhangsan",
            "age": "18"
          }
        },
        "response": {
          "status": 200,
          "json": {
            "zhangsan": "success",
            "status": "1"
          }
        }
      }
    ]
    

    headers

    [
      {
        "description": "这是带header信息的post请求",
        "request": {
          "uri": "/post/with/headers",
          "method": "post",
          "headers": {
            "content-type": "application/json"
          },
          "json": {
            "name": "zhangsan",
            "age": "19"
          }
        },
        "response": {
          "json": {
            "zhangsan": "success",
            "status": "1"
          }
        }
      }
    ]
    
    
    

    重定向

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

    url

    [
      {
        "description": "这是约定请求url",
        "request": {
          "uri": {
            "match": "/\w*/mock"
          }
        },
        "response": {
          "text": "test"
        }
      },
      {
        "description": "这是约定请求url",
        "request": {
          "uri": {
            "startsWith": "/mock"
          }
        },
        "response": {
          "text": "startsWith"
        }
      },
      {
        "description": "这是约定请求url",
        "request": {
          "uri": {
            "endsWith": "mock"
          }
        },
        "response": {
          "text": "endsWith"
        }
      },
      {
        "description": "这是约定请求url",
        "request": {
          "uri": {
            "contain": "mock"
          }
        },
        "response": {
          "text": "contain"
        }
      }
    ]
    

    易错点:
    uri 的 /
    queries form json

  • 相关阅读:
    Oracle sql的基本优化写法和思路。
    Linux的简单介绍和开发基本运维时候用到的命令
    Nginx的使用(反向代理,负载均衡)
    Mybatis传值为空需要配置JdbcType来解决吗?(XML文件不需要配置JdbcType)
    Mybatis Blob和String互转,实现文件上传等。
    Ckeditor上传图片返回的JS直接显示出来,未执行!!!
    学习中的错误——ubuntu 14.04 LTS 启动eclipse报错
    2016计算机大会后记——机器学习:发展与未来
    2016计算机大会后记——大数据时代的模式识别
    近期编程问题——epoll failed:bad file descriptor
  • 原文地址:https://www.cnblogs.com/erinchen/p/11768332.html
Copyright © 2011-2022 走看看