IDEA使用自带HttpClient代替Postman操作

背景

在日常的开发中,我们将业务功能开发好和前端进行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后缀
在这里插入图片描述

  • 7
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值