今日重点Java注解 Java反射 自定义@Depends注解
解决问题上篇讲了接口场景化用例中的2个问题: 今天我们来说说另一个问题,接口参数数据依赖问题; 解决方案1.业务 生成订单接口 { "phone": "0800485801", "appid": "1000000001", "amount": "5800", "trans_type": "01", "mchnt_no": "309040889121950" }
查询订单状态 { "appid": "10000001", //GenOrderService接口请求参数 "order_id": "18053000313296", //GenOrderService接口返回参数 "coord_longitude": "121.563352", "coord_latitude": "31.24005" }
订单支付 { "appid": "10000001", //GenOrderService接口请求参数 "order_id": "18053000313296", //GenOrderService接口返回参数 "coord_longitude": "121.563347", "coord_latitude": "31.240055", "field8583": "60000900006001003100170", "pay_type": "01" }
上述接口场景是订单支付场景,GetPayStatusService和SwitchPospDataService接口请求参数中都依赖GenOrderService接口返回的订单号,请求参数中依赖GenOrderService接口请求参数中的appid; 2.方案 3.注解 @Depends({@Depend(rely = "GenOrderService",rule = "$response.$..order_id=$..order_id"), @Depend(rely = "GenOrderService",rule = "$request.$..appid=$..appid")})
import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; import static java.lang.annotation.ElementType.METHOD; import static java.lang.annotation.ElementType.TYPE;
@Target({METHOD,TYPE}) @Retention(RetentionPolicy.RUNTIME) public @interface Depends { Depend[] value(); }
import java.lang.annotation.*; import static java.lang.annotation.ElementType.METHOD; import static java.lang.annotation.ElementType.TYPE;
@Target({METHOD,TYPE}) @Retention(RetentionPolicy.RUNTIME) public @interface Depend { String rely(); String rule(); }
@Depends:注解主要是解决接口场景化过程中接口数据依赖问题,处理多个接口参数依赖问题;
@Depend:注解具体解决依赖参数; rely:表示依赖接口 rule:依赖规则处理 rule = "$response.$..order_id=$..order_id",$response:规则是将依赖接口的返回报文中order_id字段内容注入到当前接口order_id参数中; rule = "$request.$..appid=$..appid",$request:规则是将依赖接口的请求报文中appid字段内容注入到当前接口appid参数中; 4.Java反射 反射主要的功能 实现方案 注:@Depends反射解析代码暂不提供,此代码需要依赖框架执行; 5.总结 接口场景化 通过@DisabledOnScenario和@Depends注解我们解决了接口场景化用例的痛点;如果在接口场景化测试过程中还有哪些问题欢迎公众号留言;
|