0

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?

pcsutar
  • 1,715
  • 2
  • 9
  • 14
Igor Vlasuyk
  • 514
  • 2
  • 10
  • 22
  • 1
    you can check this thread: https://stackoverflow.com/questions/21725093/rest-assured-deserialize-response-json-as-listpojo – kmcakmak Aug 15 '21 at 19:18

2 Answers2

0

kmcakmak has pointed to the solution. Here is a code:

public <T> List<T> getBodyAsListOf(Class<T> aClass) {
    JsonPath jsonPath = response.getBody().jsonPath();
    return jsonPath.getList("", aClass);
}
Igor Vlasuyk
  • 514
  • 2
  • 10
  • 22
0

The nicest version I have found is below. A generic type of the list is inferred from the return type automatically.

private static List<LedgerAccountDto> getAllLedgerAccount() {
    return given()
            .when().get("/wallet/ledger_accounts/")
            .then()
            .statusCode(200)
            .extract().as(new TypeRef<>() {});
}

There is no error or warning in InteliJ or SonarLint, so it seems to be the best solution.

And it works great with a template method too

private static <T> List<T> getAllLedgerAccount() {
    return given()
            .when().get("/wallet/ledger_accounts/")
            .then()
            .statusCode(200)
            .extract().as(new TypeRef<>() {});
}

used this way

   List<LedgerAccountDto> fetchedAccounts = getAllLedgerAccount();

This time also no warnings or errors due to the generic type automatically calculated by java.

Marek Żylicz
  • 429
  • 3
  • 8