英文:
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 "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"}}
答案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<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);
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论