java自动化测试-mock

1.下载jar包
https://repo1.maven.org/maven2/com/github/dreamhead/moco-runner/0.11.0
下载 moco-runner-0.11.0-standalone.jar
2.开发
2.0 将下载jar包放到工程中
2.1 新建文件startup1.json,内容如下:

[
{
“description”:“这是我们第一个mock例子”,
“request”:{
“uri”:"/demo"
},
“response”:{
“text”:“第一个moco框架demo”
}
}
]

2.2 运行以下命令,即可运行mock

jar ./moco-runner-0.11.0-standalone.jar http -p 8888 -c startup1.json

2.3 测试,在浏览器中打开地址 http://localhost:8888/demo
2.4访问成功

mock常用的基本使用

[
	{
		"description":"模拟一个没有参数的get请求",
		"request":{
			"uri":"/getdemo",
			"method":"get"
		},
		"response":{
			"text":"结果1111"
		}
	},
	{
		"description":"模拟一个有参数的get请求",
		"request":{
			"uri":"/getwithdemo",
			"method":"get",
			"queries":{
				"name":"xxx",
				"age":"19"
			}
		},
		"response":{
			"text":"xxx回来了"
		}
	},
	{
		"description":"模拟一个没参数的post请求",
		"request":{
			"uri":"/postdemo",
			"method":"post",
			"queries":{
				"name":"xxx",
				"age":"19"
			}
		},
		"response":{
			"text":"xxx回来了"
		}
	},
	{
		"description":"模拟一个有参数的post请求",
		"request":{
			"uri":"/postdemo",
			"method":"post",
			"forms":{
				"name":"xxx",
				"age":"19"
			}
		},
		"response":{
			"text":"post回来了"
		}
	},
	{
		"description":"带cookies信息的get请求",
		"request":{
			"uri":"/postdemo",
			"method":"post",
			"cookies":{
				"loin":"true"
			}
		},
		"response":{
			"text":"cookies回来了"
		}
	},
	{
		"description":"带cookies信息的post请求",
		"request":{
			"uri":"/postdemo/cookies",
			"method":"post",
			"cookies":{
				"loin":"true"
			},
			"json":{
				"name":"huhansan",
				"age":"18"
			}
		},
		"response":{
			"status":200,
			"json":{
				"huhansan":"success",
				"status":"1"
			}
		}
	},
	{
		"description":"带header信息的post请求",
		"request":{
			"uri":"/postdemo/headers",
			"method":"post",
			"headers":{
				"content-type":"application/json"
			},
			"queries":{
				"name":"huhansan",
				"age":"18"
			}
		},
		"response":{
			"status":200,
			"json":{
				"huhansan":"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":"重定向成功啦"
		}
	}
]

你可能感兴趣的:(java自动化测试,java)