接口自动化-如何对多参数接口进行任意参数个数传参-基于Java建造者设计模式

接口

我假设有这样一个接口:

  • 方法:post
  • 功能:根据不同的条件参数,如姓名、年龄、性别、受教育程度等来查询筛选患者用户
  • Body:
{
  "educationTime": "string",
  "jobType": "string",
  "marrige": "string",
  "medicalHistory": 0,
  "medicationName": "string",
  "orderBy": "string",
  "pageNumber": 0,
  "pageSize": 100,
  "patientAge": "string",
  "patientName": "string",
  "patientSex": 0,
  "sortKey": "string"
}

需求

  • 接口请求实现:请求参数传入map,然后利用遍历map来修改json文件中的值,最终完成请求体body,传入jsonString进行接口请求,实现患者信息的创建;
public Response createPatient(HashMap<String,Object> map){
        map.put("_file", "/data/assessmentapp/patientManager/createPatient.json");
        return getResponseFromYaml(
                "/api/brainPFApp/patientManager/createPatient.yaml",
                map,
                tokenPattern
        );
    }
  • 用例实现:由于进行筛选的信息很多,排列组合起来非常复杂,因此想要设计一个测试用例可以任意输入参数,也就是任意输入患者的姓名、年龄、性别等信息作为参数进行接口自动化的测试工作。

设计模式实现

  • 以建造者模式设计不同参数的处理方法,这里因为我要将参数传入map,所以不同的参数对应了不同的key,value集合传入map,最终以一个buildPatient方法将对象返回:
package com.appApi.assessmentapp.interfance;

import lombok.Data;

import java.util.HashMap;

@Data
public class CreatePatient {
    private HashMap<String,Object> map;


    public static class CreatePatientBuilder {
        HashMap<String,Object> map1 = new HashMap<>();

        private CreatePatient createPatient;
        public CreatePatientBuilder(){
            createPatient = new CreatePatient();
        }

        public CreatePatientBuilder buildEducation(String education){
            map1.put("$.patient.education",education);
            createPatient.setMap(map1);
            return this;
        }

        public CreatePatientBuilder buildEducationTime(Integer educationTime){
            map1.put("$.patient.educationTime",educationTime);
            createPatient.setMap(map1);
            return this;
        }

        public CreatePatientBuilder buildJobType(String jobType){
            map1.put("$.patient.jobType",jobType);
            createPatient.setMap(map1);
            return this;
        }

        public CreatePatientBuilder buildMarrige(String marrige){
            map1.put("$.patient.marrige",marrige);
            createPatient.setMap(map1);
            return this;
        }

        public CreatePatientBuilder buildMobilePhone(String mobilePhone){
            map1.put("$.patient.mobilephone",mobilePhone);
            createPatient.setMap(map1);
            return this;
        }

        public CreatePatientBuilder buildPatientAge(Integer patientAge){
            map1.put("$.patient.patientAge",patientAge);
            createPatient.setMap(map1);
            return this;

        }

        public CreatePatientBuilder buildPatientBirthdate(String patientBirthdate){
            map1.put("$.patient.patientBirthdate",patientBirthdate);
            createPatient.setMap(map1);
            return this;
        }

        public CreatePatientBuilder buildPatientName(String patientName){
            map1.put("$.patient.patientName",patientName);
            createPatient.setMap(map1);
            return this;
        }

        public CreatePatientBuilder buildAddress(String address){
            map1.put("$.patient.address",address);
            createPatient.setMap(map1);
            return this;
        }

        public CreatePatientBuilder buildPatientSex(Integer patientSex){
            map1.put("$.patient.patientSex",patientSex);
            createPatient.setMap(map1);
            return this;
        }

        public CreatePatient buildPatient(){
            return createPatient;
        }
    }

}


  • 说明
    1) 在CreatePatient类中,有一个内部静态类CreatePatientBuilder,我们使用这个类的时候就会加载它的构造方法CreatePatientBuilder实例化一个CreatePatient
    2) 而每一个build方法(比如buildNamebuildAge等)最终都会返回this本身(静态类CreatePatientBuilder),所以我们可以不断利用.buildXXX.buildXXX的形式来进行赋值;
    3) 最终可以以一个buildPatient方法来返回整个对象,由于我需要map的值,所以我这里就可以通过CreatePatient类来最终获取map的值(其实这里在buildPatient直接返回map1也可以)

用例实现

/**
     * 创建患者必填项
     *     address	string
     *     education*	string
     *     educationTime*	integer($int32)
     *     jobType*	string
     *     marrige*	string
     *     mobilephone*	string
     *     patientBirthdate*	string
     *     patientName*	string
     *     patientSex*	integer($int32)
     */
    @Test
    void createPatient(){
        String patientName = randomValueUtil.getRandomName();
        String phoneNumber = randomValueUtil.getRandomPhoneNumber();
        String patientBirthDate = randomValueUtil.getRandomBirthDate();
        Integer educationTime = randomValueUtil.getNum(0,22);
        CreatePatient buildPatient = new CreatePatient.CreatePatientBuilder()
                .buildPatientName(patientName)
                .buildMobilePhone(phoneNumber)
                .buildPatientBirthdate(patientBirthDate)
                .buildEducationTime(educationTime)
                .buildAddress("上海市浦东新区张江镇")
                .buildPatient();
        HashMap<String, Object> map = buildPatient.getMap();
        System.out.println(map);
        patientManager.createPatient(map).then().statusCode(200)
                .body("status",equalTo("1"))
                .body(matchesJsonSchemaInClasspath("responseSchema/assessmentapp/patientManager/createPatient.schema"));
  • 运行打印map结果:
{$.patient.mobilephone=130****2175, $.patient.patientName=向X艳, $.patient.address=上海市浦东新区张江镇, $.patient.patientBirthdate=1919-12-10, $.patient.educationTime=8}
这样就可以完成任意的传参,在接口自动化中使测试用例的灵活拓展性更强,可覆盖程度也可更高。
  • 1
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值