前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Java自动化测试(mock 21)

Java自动化测试(mock 21)

作者头像
zx钟
发布2020-08-21 17:11:44
4910
发布2020-08-21 17:11:44
举报
文章被收录于专栏:测试游记

mock测试就是在测试过程中,对于某些不容易构造或者不容易获取的对象,用一个虚拟的对象来创建以便测试的测试方法。

mock测试环境

https://www.easy-mock.com/login https://github.com/easy-mock/easy-mock

Easy Mock 是一个可视化,并且能快速生成模拟数据的持久化服务。

特性

  • 支持接口代理
  • 支持快捷键操作
  • 支持协同编辑
  • 支持团队项目
  • 支持 RESTful
  • 支持 Swagger | OpenAPI Specification (1.2 & 2.0 & 3.0)
  • 基于 Swagger 快速创建项目
  • 支持显示接口入参与返回值
  • 支持显示实体类
  • 支持灵活性与扩展性更高的响应式数据开发
  • 支持自定义响应配置(例:status/headers/cookies)
  • 支持 Mock.js 语法
  • 支持 restc 方式的接口预览

登陆

单机版

https://github.com/dreamhead/moco

Maven

代码语言:javascript
复制
<project xmlns="http://maven.apache.org/POM/4.0.0"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
   <modelVersion>4.0.0</modelVersion>
   <groupId>com.lemon</groupId>
   <artifactId>moco-runner</artifactId>
   <version>0.0.1-SNAPSHOT</version>
   <properties>
      <maven.compiler.target>1.8</maven.compiler.target>
      <maven.compiler.source>1.8</maven.compiler.source>
      <!-- 文件拷贝时的编码 -->
      <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
      <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
      <!-- 编译时的编码 -->
      <maven.compiler.encoding>UTF-8</maven.compiler.encoding>
   </properties>
   <dependencies>
      <dependency>
         <groupId>com.github.dreamhead</groupId>
         <artifactId>moco-runner</artifactId>
         <version>1.0.0</version>
      </dependency>
   </dependencies>

</project>

runner.java

代码语言:javascript
复制
package com.lemon;

import com.github.dreamhead.moco.bootstrap.Main;

public class Runner {
   public static void main(String[] args) {
      test9();
   }

   public static void test1() {
      // get http://localhost:12306/
      String[] args = "http -p 12306 -c src/test/resources/foo.json".split(" ");
      Main.main(args);
   }

   public static void test2() {
      // get http://localhost:12306/ body foo
      String[] args = "http -p 12307 -c src/test/resources/foo2.json".split(" ");
      Main.main(args);
   }

   public static void test3() {
      // get http://localhost:12306/getBoy?name=onecoder
      String[] args = "http -p 12306 -c src/test/resources/foo3.json".split(" ");
      Main.main(args);
   }

   public static void test4() {
      // post http://localhost:12306/getBoy body name=onecoder
      String[] args = "http -p 12306 -c src/test/resources/foo4.json".split(" ");
      Main.main(args);
   }

   public static void test5() {
      // get http://localhost:12306/template?name=123 返回 123
      String[] args = "http -p 12306 -c src/test/resources/foo5.json".split(" ");
      Main.main(args);
   }

   public static void test6() {
      // get http://localhost:12306/template?name=123 返回 123
      String[] args = "http -p 12306 -c src/test/resources/foo6.json".split(" ");
      Main.main(args);
   }

   public static void test7() {
      // get http://localhost:12306/template?name=123 返回 123
      String[] args = "http -p 12306 -c src/test/resources/foo7.json".split(" ");
      Main.main(args);
   }

   public static void test8() {
      // get http://localhost:12306/template?name=123 返回 123
      String[] args = "http -p 12306 -c src/test/resources/foo8.json".split(" ");
      Main.main(args);
   }

   public static void test9() {
      // get http://localhost:12306/template?name=123 返回 123
      String[] args = "http -p 12306 -c src/test/resources/foo9.json".split(" ");
      Main.main(args);
   }
}

测试数据

  • foo1.json
代码语言:javascript
复制
[
  {
    "response" :
      {
        "text" : "Hello, Moco"
      }
  }
] 
  • foo2.json
代码语言:javascript
复制
[{
  "request" : { "text" : "foo" },
  "response" : {  "xml" : "bar" }
}]
  • foo3.json
代码语言:javascript
复制
[{
   "request": {
      "uri": "/getBoy",
      "queries": {
         "name": "onecoder"
      }
   },
   "response": {
      "text": "get Hey."
   }
}]
  • foo4.json
代码语言:javascript
复制
[
   {
      "request": {
         "method": "post",
         "forms": {
            "name": "onecoder"
         }
      },
      "response": {
         "text": "post Hi."
      }
   }
]
  • foo5.json
代码语言:javascript
复制
[
   {
      "request": {
         "uri": "/template"
      },
      "response": {
         "text": {
            "template": "aaaaaaaa${req.queries['name']}bbbbbbb"
         }
      }
   }
]
  • foo6.json
代码语言:javascript
复制
[
   {
      "description": "带header请求",
      "request": {
         "uri": "/member/register",
         "method": "post",
         "headers": {
            "content-type": "application/json"
         },
         "json": {
            "mobile_phone": "13212312312",
            "pwd": "12345678"
         }
      },
      "response": {
         "json": {
            "code": 0,
            "msg": "OK",
            "data": {
               "id": 189,
               "reg_name": "小柠檬",
               "mobile_phone": "13212312312"
            }
         }
      }
   }
]
  • foo7.json
代码语言:javascript
复制
[
   {
      "description": "这是一个带cookies的Post请求",
      "request": {
         "uri": "/postDemoWithCookies",
         "method": "post",
         "cookies": {
            "login": "true"
         }
      },
      "response": {
         "status": "200",
         "json": {
            "name": "success",
            "status": "1"
         },
         "cookies": {
            "JSESSIONID": "123"
         }
      }
   }
]
  • foo8.json
代码语言:javascript
复制
[
   {
      "description": "充值",
      "request": {
         "uri": "/member/recharge",
         "method": "post",
         "headers": {
            "X-Lemonban-Media-Type": "lemonban.v2",
            "Authorization" : "Bearer Token",
            "content-type": "application/json"
         },
         "json": {
            "member_id": "123",
            "amount": "4000"
         }
      },
      "response": {
         "json": {
            "code": 0,
            "msg": "OK",
            "data": {
               "id": 123,
               "leave_amount": 1800000.01 ,
               "mobile_phone": "13888888879",
               "reg_name": "小柠檬",
               "reg_time": "2019-08-25 21:18:30.0",
               "type": 1
            }
         }
      }
   }
]
  • foo9.json
代码语言:javascript
复制
[
   {
      "description": "登录",
      "request": {
         "uri": "/member/login",
         "method": "post",
         "headers": {
            "X-Lemonban-Media-Type": "lemonban.v2",
            "content-type": "application/json;charset=utf-8"
         },
         "json": {
            "mobile_phone": "13212312312",
            "pwd": "12345678"
         }
      },
      "response": {
         "json": {
            "code": 0,
            "msg": "OK",
            "data": {
               "id": 70362,
               "leave_amount": 0.0,
               "mobile_phone": "13212312312",
               "reg_name": "小柠檬",
               "reg_time": "2019-12-19 21:12:21.0",
               "type": 1,
               "token_info": {
                  "token_type": "Bearer",
                  "expires_in": "2019-12-19 21:17:23",
                  "token": "eyJhbGciOiJIUzUxMiJ9.eyJtZW1iZXJfaWQiOjg5ODU4LCJleHAiOjE1NzY3NjE0NDN9.ZJKz-aOxWtCQakvhOfRjTSTA_K7amiKe0e9wpaF9lpzKQaTZiVjsoUDmpA2DCKtIdMyaQ4QYWWybP1ZD5QDVWQ"
               }
            },
            "copyright": "Copyright 柠檬班 © 2017-2019 湖南省零檬信息技术有限公司 All Rights Reserved"
         }
      }
   }
]
本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2020-08-15,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 测试游记 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • mock测试环境
    • 特性
    • 单机版
      • Maven
        • runner.java
          • 测试数据
          领券
          问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档