mock测试就是在测试过程中,对于某些不容易构造或者不容易获取的对象,用一个虚拟的对象来创建以便测试的测试方法。
https://www.easy-mock.com/login https://github.com/easy-mock/easy-mock
Easy Mock 是一个可视化,并且能快速生成模拟数据的持久化服务。
登陆
https://github.com/dreamhead/moco
<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>
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
[
{
"response" :
{
"text" : "Hello, Moco"
}
}
]
foo2.json
[{
"request" : { "text" : "foo" },
"response" : { "xml" : "bar" }
}]
foo3.json
[{
"request": {
"uri": "/getBoy",
"queries": {
"name": "onecoder"
}
},
"response": {
"text": "get Hey."
}
}]
foo4.json
[
{
"request": {
"method": "post",
"forms": {
"name": "onecoder"
}
},
"response": {
"text": "post Hi."
}
}
]
foo5.json
[
{
"request": {
"uri": "/template"
},
"response": {
"text": {
"template": "aaaaaaaa${req.queries['name']}bbbbbbb"
}
}
}
]
foo6.json
[
{
"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
[
{
"description": "这是一个带cookies的Post请求",
"request": {
"uri": "/postDemoWithCookies",
"method": "post",
"cookies": {
"login": "true"
}
},
"response": {
"status": "200",
"json": {
"name": "success",
"status": "1"
},
"cookies": {
"JSESSIONID": "123"
}
}
}
]
foo8.json
[
{
"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
[
{
"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"
}
}
}
]