如何创建具有多个键参数值的查询参数主体

How to create query param body with multiple key param values

在下面的散列图中,您可以看到,我有关键参数值列表,我需要为多个值自动处理案例,而不是重复散列图,而是更新。

我是怎么做的:

第一个测试用例

HashMap<String, String> queryParam = new HashMap<>();
queryParam.put("Name", Name);
queryParam.put("street","street" );
queryParam.put("city","city" );
queryParam.put("state", "state");
queryParam.put("postalCode","postalCode" );
queryParam.put("country", "country");
queryParam.put("email", "email");
queryParam.put("website","website" );
queryParam.put("phone", "phone");

Response response = request.auth().basic(uname, pwd).body(queryParam).contentType(APPLICATION_JSON)
            .post().then().extract()
            .response();

现在如果你看到上面的 hashmap,它有强制参数,一些可选参数,然后每个参数都有不同的验证。现在它用每个键覆盖所有测试用例,上面的 haspmap 是重复的,值或键是变化的。我想以更好、更有效的方式来做到这一点。

您应该使用 Java POJO,而不是使用 Map。使用构造函数设置默认值,然后使用 setter 更改值。效率更高。

另外,您可以应用工厂设计模式为每个测试用例构建具有所需值的对象。

测试示例

@Test
void test1() {
    QueryObject query = QueryObjectFactory.getDefaultValue();

    Response res = given().contentType(ContentType.JSON)
            .body(query)
            .post("to_your_api_endpoint");
}

工厂class

public class QueryObjectFactory {

    public static QueryObject getDefaultValue() {
        QueryObject queryObject = new QueryObject();
        queryObject.setName("name");
        queryObject.setStreet("street");
        queryObject.setCity("city");
        queryObject.setCountry("country");
        queryObject.setState("state");
        queryObject.setPostalCode("postalCode");
        queryObject.setEmail("email");
        queryObject.setWebsite("website");
        queryObject.setPhone("phone");
        return queryObject;
    }
}

POJO 注意:我使用 lombok 生成 getter 和 getter --> 减少复杂的 POJO class.

import lombok.Data;

@Data
public class QueryObject {
    private String name;
    private String street;
    private String city;
    private String state;
    private String postalCode;
    private String country;
    private String email;
    private String website;
    private String phone;
}