0

In the below hashmap you can see, I have list of key param values for which I need to automate cases for multiple values without repeating the hashmap rather it would be and update.

How I am doing it:

1st test case

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();

Now if you see the above hashmap, it has mandatory params, some optional and then each param has different validation. Now it terms to cover all the testcases with each keys, above haspmap is repeating and values or keys are changing. I would like to do this in better and efficient way of it.

4
  • It is not clear to me what you are trying to do. You code snippet is not a test case - it just shows you making a HTTP call and passing some URL parameters to it. Are you testing the receiver of the request? what are you trying to test on it? How does variable/optional parameters come into it? Commented Jun 4, 2021 at 14:54
  • When a api testcase will be executed will verify the different validation combination and error codes. To do this, above sample request is created. Question is if same needs to be done, How many more hashmaps needs to be created to create multiple testcases for this request. Does it make sense now? Commented Jun 6, 2021 at 15:08
  • No, sorry - it still doesn't make much sense. You seem to be asking for advice on how many/what test cases you need to write (or how better to write them) to meet some unknown requirements. Without more details on the functionality you are exercising I don't think we can help you. It also sounds like this is more of a question for the code review site, not stack overflow? Commented Jun 11, 2021 at 21:31
  • No, I am not asking how many test cases needs to be created, I am saying I unable to create a generic haspmap to test multiple scenarios while changing the hash map key pair values. Thanks for trying. Commented Jun 12, 2021 at 15:50

1 Answer 1

1

Instead of using Map<K, V>, you should use Java POJO. Using constructor to setup default value, then using setter to change value. It's more efficient.

One more thing, you could apply Factory design pattern to build object with desired value for each test case.

Test example

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

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

Factory 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 note: I use lombok to generate getter and getter --> reduce complex of 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;
}
Sign up to request clarification or add additional context in comments.

2 Comments

Question- To how the data will change for lombok. I never used it so, guidance would be appreciated.
Lombok will generate code such as getter, setter, constructor, toString, hashCode... at run time based on the annotation you put into class. Basically, it helps me to make POJO less verbose.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.