背景
在日常的开发中,我们将业务功能开发好和前端进行API联调之前往往需要开发者先对API进行一次自测,确保api是可用的,个人认为标准的做法应该是写单元测试,但是单元测试又比较耗费时间,所以大多数人选择使用postman进行自测,postman自测后方法虽然可以保存,但是随着业务增大,方法变多,postman保存的方法越来越多,使用起来速度会变的比较忙,本文给大家介绍一下使用IDEA字段httpClient代替postman进行自测.
前提条件
1.使用Idea旗舰版
2.在测试的模块即api层添加evn配置文件
写测试请求
1.POST
POST http://{{host}}/V0/products
Content-Type: application/json
{
"name": "测试",
"description": "测试描述",
"category": "花卉"
}
> {%
client.test("Request executed successfully",function (){
client.assert(response.status===200,"Get Collection failed,response:"+response.body)
});
%}
2.PUT
PUT http://{{host}}/V0/products
Content-Type: application/json
{
"id": 1,
"name": "测试",
"description": "测试描述",
"category": "花卉"
}
> {%
client.test("Request executed successfully",function (){
client.assert(response.status===200,"Get Collection failed,response:"+response.body)
});
%}
3.GET
GET http://{{host}}/V0/products/1
Content-Type: application/json
> {%
client.test("Request executed successfully",function (){
client.assert(response.status===200,"Get Collection failed,response:"+response.body)
});
%}
4.GET列表
GET http://{{host}}/V0/products?name=纸巾&pageNumber=1&pageSize=10
Content-Type: application/json
> {%
client.test("Request executed successfully",function (){
client.assert(response.status===200,"Get Collection failed,response:"+response.body)
});
%}
5.DELETE
DELETE http://{{host}}/V0/products/1
Content-Type: application/json
> {%
client.test("Request executed successfully",function (){
client.assert(response.status===200,"Get Collection failed,response:"+response.body)
});
%}
运行
如果运行的时候配置的环境不能显示出来(如上图),要打开idea配置,添加.json后缀