mock测试

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

用到的主要工具就是  Mock Server。

链接:http://www.mock-server.com/where/downloads.html

这是一个jar包,启动需要java环境

 

1、启动服务,先要添加一个配置文件config.json(与jar包在同一目录下)

config.json

复制代码
[
    {
        "request" :
        {
            "uri" : "/posts",
            "method" : "post",
            "file" : {
                "json" : "creat_post.json"
            }
        },
        "response" :
        {
            "file" : "posts.json"
        }
    },
    {    
        "request" :
        {
            "uri" : "/gets",
            "method" : "get"
        },
        "response":
        {
            "file" : "gets.json"
        }
    },
    {
        "request" : {
            "uri" : "/edit",
            "method" : "put",
            "file" : {
                "json" : "creat_post.json"
            }
        },
        "response" :
        {
            "json" : {"success" : "true"}
        }    
    },
    {
        "request": {
            "uri" : "/delete",
            "method" : "delete"
        },
        "response":
        {
            "json" : {"success" : "true"}
        }
    }
]
复制代码

creat_post.json

1
2
3
4
{
    "new" : "QQ" ,
    "old" : "taobao"
}

 posts.json

1
2
3
4
5
6
7
8
9
10
[
    {
        "fruit" : "apple",
        "computer" : "lenvo"
    },
    {
        "mobile" : "iphone",
        "book" : "testing"
    }
]

 gets.json

1
2
3
4
5
6
7
8
9
10
[
    {
        "title" : "java",
        "version" : "1.8.0"
    },
    {
        "title" : "python",
        "version" : "3.5"
    }
]

 这里配置四个接口,对应http的四种请求类型 get、post、put、delete.

 

2、进入目录,cmd命令启动。

java -jar moco-runner-0.10.0-standalone.jar http -p 12306 -c config.json

 

http 是接口的类型

-p 端口号

-c 配置文件

 

3、验证下接口

  

简单的接口模拟已经成功了。

 

posted @   君何在  阅读(437)  评论(0编辑  收藏  举报
编辑推荐:
· 软件产品开发中常见的10个问题及处理方法
· .NET 原生驾驭 AI 新基建实战系列:向量数据库的应用与畅想
· 从问题排查到源码分析:ActiveMQ消费端频繁日志刷屏的秘密
· 一次Java后端服务间歇性响应慢的问题排查记录
· dotnet 源代码生成器分析器入门
阅读排行:
· ThreeJs-16智慧城市项目(重磅以及未来发展ai)
· 软件产品开发中常见的10个问题及处理方法
· Vite CVE-2025-30208 安全漏洞
· 互联网不景气了那就玩玩嵌入式吧,用纯.NET开发并制作一个智能桌面机器人(四):结合BotSharp
· MQ 如何保证数据一致性?
点击右上角即可分享
微信分享提示