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
{ "new" : "QQ" , "old" : "taobao" }
posts.json
[ { "fruit" : "apple", "computer" : "lenvo" }, { "mobile" : "iphone", "book" : "testing" } ]
gets.json
[ { "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、验证下接口
简单的接口模拟已经成功了。
原文:http://www.cnblogs.com/waylon/p/6697068.html