0

Im working on Rest Assured project and I want to get speciific part of response body but Im getting exception:

Exception in thread "main" java.lang.ClassCastException: class io.restassured.path.xml.XmlPath cannot be cast to class java.util.ArrayList (io.restassured.path.xml.XmlPath is in unnamed module of loader 'app'; java.util.ArrayList is in module java.base of loader 'bootstrap')

The code is taken from tutorial website so it should works correctly. Can anyone help?

 public static void getSpecificPartOfResponseBody(){
        ArrayList<String> amounts = when().get(url).then().extract().path("result.statements.AMOUNT") ;
        int sumOfAll=0;
        for(String a:amounts){

            System.out.println("The amount value fetched is "+a);
            sumOfAll=sumOfAll+Integer.valueOf(a);
        }
        System.out.println("The total amount is "+sumOfAll);

}

response body:

{"result:":{"statements":[{"TRANSACTION_ID":"12","ACCOUNT_NO":"1","DATE_OF_TRANSACTION":"2013-11-16","AMOUNT":"500","TRANSACTION_TYPE":"D","DESCRIPTION":"Initial Deposit"},{"TRANSACTION_ID":"23","ACCOUNT_NO":"1","DATE_OF_TRANSACTION":"2013-11-17","AMOUNT":"14","TRANSACTION_TYPE":"t","DESCRIPTION":"yi Tansfer From 14"},{"TRANSACTION_ID":"25","ACCOUNT_NO":"1","DATE_OF_TRANSACTION":"2013-11-18","AMOUNT":"1","TRANSACTION_TYPE":"t","DESCRIPTION":"hgg Tansfer From 15"},{"TRANSACTION_ID":"49745","ACCOUNT_NO":"1","DATE_OF_TRANSACTION":"2017-04-13","AMOUNT":"0","TRANSACTION_TYPE":"t","DESCRIPTION":"0 Tansfer From 1"},{"TRANSACTION_ID":"94867","ACCOUNT_NO":"1","DATE_OF_TRANSACTION":"2018-11-21","AMOUNT":"500","TRANSACTION_TYPE":"t","DESCRIPTION":"cash Tansfer From 14"}]},"message":{"ErrorCode:":0,"ErrorMsg:":"Success"}}
Kos
  • 1
  • 1
  • 2
  • 2
    Please add the complete response body to the question. – kaweesha Oct 07 '20 at 06:34
  • 1
    They key `result` has a `:` in the response, either the response is incorrect which you will have to check with the developers or you could use `path("'result:'.statements.AMOUNT")` – Wilfred Clement Oct 07 '20 at 18:55
  • @WilfredClement Response body comes from tutorial so it should be properly. I've changed `path("'result:'.statements.AMOUNT")` as you mentioned, but exception still occurs. – Kos Oct 08 '20 at 05:14
  • 1
    @Kos - I agree with @WilfredClement. The `:` in `result:` is a mistake. But, it I ran the complete above code with `path("'result:'.statements.AMOUNT")`. And I got the expected output correctly. So I think the issue is in imports. Add only `io.restassured.specification.RequestSpecification`, `java.util.ArrayList` and `io.restassured.RestAssured.when` as imports and rerun the method. I think, you will get the expected output. If this doesn't work please add the imports and your rest assured, java versions to the question. – kaweesha Oct 17 '20 at 10:32
  • @kaweesha thank you for your answer. I added your imports, but issue still occurs. My rest assured version is 4.3.1 and I use Java version 8 Update 261. My current imports are: `import io.restassured.specification.RequestSpecification; import java.util.ArrayList; import java.util.concurrent.TimeUnit; import static io.restassured.RestAssured.*;` – Kos Oct 18 '20 at 12:38
  • How do you run this method? Can you completely isolate this method and run? – kaweesha Oct 18 '20 at 14:05

1 Answers1

1

The url is a php and the contenttype is not json but HTML. path(...) only works with json. You can use the idea for another URL where the contenttype is json. I used this one: https://dummy.restapiexample.com/api/v1/employees with this code:

public static void getSpecificPartOfResponseBody(){

    ArrayList<Integer> ages;
    ages = when().get(url).then().extract().path("data.employee_age");


    int sumOfAll=0;
    for(Integer a :ages){

        System.out.println("The age value fetched is "+a);
        sumOfAll=sumOfAll+ a;
    }
    System.out.println("The total amount of ages is " + sumOfAll);
}