I have a test automation pet project with RestAssured, which I want to make architecturally developed, in order to make code reusable and test scripts easy to read. I have classes that represent requests and responses of API, and also I have DTO classes. For example here is a class that works with the endpoint:
public class BreedsEndpoint extends EndpointTechnicalSteps{
public BreedsEndpoint(){
super();
}
public BreedsEndpoint search(RequestSpecification spec, String nameOfBreed) {
Response response = given().spec(spec)
.param("q", nameOfBreed)
.when()
.get("breeds/search/")
.thenReturn();
this.response = response;
return this;
}
}
As you see there is a parent class EndpointTechnicalSteps
which I want to use to handle responses from the endpoint. For example, I want to make a method in EndpointTechnicalSteps
which will return a List of objects from the response, and here is a problem. The last version of RestAssured supports only such decision
Object[] objectArray = response.getBody().as(Object[].class);
where I have to pass Object[].class
as parameter.
Here is code of EndpointTechnicalSteps
class
public class EndpointTechnicalSteps {
protected Response response;
public <T> T getBodyAsList(Class<T> aClass) {
return response.getBody().as(aClass);
}
}
And if I invoke, for example, getBodyAsList
method with Breed[].class
parameter , then I get array Breed[] breeds
How to make a method in EndpointTechnicalSteps that will return <T> List<T>
i.e. list of objects, but not array?