Rest Assured异常类io.restassured.path.xml.XmlPath无法转换为类java.util.ArrayList错误。

huangapple go评论82阅读模式
英文:

Rest Assured execption class io.restassured.path.xml.XmlPath cannot be cast to class java.util.ArrayList error

问题

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

响应主体:

{
    "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"
    }
}
英文:

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

Exception in thread &quot;main&quot; 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 &#39;app&#39;; java.util.ArrayList is in module java.base of loader &#39;bootstrap&#39;)

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

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

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

}

response body:

{&quot;result:&quot;:{&quot;statements&quot;:[{&quot;TRANSACTION_ID&quot;:&quot;12&quot;,&quot;ACCOUNT_NO&quot;:&quot;1&quot;,&quot;DATE_OF_TRANSACTION&quot;:&quot;2013-11-16&quot;,&quot;AMOUNT&quot;:&quot;500&quot;,&quot;TRANSACTION_TYPE&quot;:&quot;D&quot;,&quot;DESCRIPTION&quot;:&quot;Initial Deposit&quot;},{&quot;TRANSACTION_ID&quot;:&quot;23&quot;,&quot;ACCOUNT_NO&quot;:&quot;1&quot;,&quot;DATE_OF_TRANSACTION&quot;:&quot;2013-11-17&quot;,&quot;AMOUNT&quot;:&quot;14&quot;,&quot;TRANSACTION_TYPE&quot;:&quot;t&quot;,&quot;DESCRIPTION&quot;:&quot;yi Tansfer From 14&quot;},{&quot;TRANSACTION_ID&quot;:&quot;25&quot;,&quot;ACCOUNT_NO&quot;:&quot;1&quot;,&quot;DATE_OF_TRANSACTION&quot;:&quot;2013-11-18&quot;,&quot;AMOUNT&quot;:&quot;1&quot;,&quot;TRANSACTION_TYPE&quot;:&quot;t&quot;,&quot;DESCRIPTION&quot;:&quot;hgg Tansfer From 15&quot;},{&quot;TRANSACTION_ID&quot;:&quot;49745&quot;,&quot;ACCOUNT_NO&quot;:&quot;1&quot;,&quot;DATE_OF_TRANSACTION&quot;:&quot;2017-04-13&quot;,&quot;AMOUNT&quot;:&quot;0&quot;,&quot;TRANSACTION_TYPE&quot;:&quot;t&quot;,&quot;DESCRIPTION&quot;:&quot;0 Tansfer From 1&quot;},{&quot;TRANSACTION_ID&quot;:&quot;94867&quot;,&quot;ACCOUNT_NO&quot;:&quot;1&quot;,&quot;DATE_OF_TRANSACTION&quot;:&quot;2018-11-21&quot;,&quot;AMOUNT&quot;:&quot;500&quot;,&quot;TRANSACTION_TYPE&quot;:&quot;t&quot;,&quot;DESCRIPTION&quot;:&quot;cash Tansfer From 14&quot;}]},&quot;message&quot;:{&quot;ErrorCode:&quot;:0,&quot;ErrorMsg:&quot;:&quot;Success&quot;}}

答案1

得分: 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);
}
英文:

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&lt;Integer&gt; ages;
    ages = when().get(url).then().extract().path(&quot;data.employee_age&quot;);


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

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

huangapple
  • 本文由 发表于 2020年10月7日 13:58:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/64238047.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定