Forum Discussion

MohammedRehman's avatar
MohammedRehman
New Member
6 years ago

ERROR: An error occurred [java.lang.ClassCastException]

Hi

 

I am trying to add a Groovy Script to compare Data Source Results with Rest API Resuest. I am asserting as a individual Step. Currently it is erroring out and the error is Java.Lang.ClassCastException.

 

If I Remove For Loop then Script passes, but i want script to go through all records in db and compare all node elements from API

 

import groovy.xml.*
import groovy.json.JsonSlurper


//Grabbing db Response
def cO_RecordID = context.expand( '${DataSource#cO_RecordID }' ) //retreive record id

def db_recordid = cO_RecordID .toString() // Casting Result to String
log.info db_recordid

//Grabbing Json Response
def Jsonresponse_RecordId = context.expand( '${Get Funding Frequencies#Response}' ) //retreiving Json Response
log.info Jsonresponse_RecordId

def Json_Object = new JsonSlurper().parseText(Jsonresponse_RecordId) // putting Json Response into an Object
log.info Json_Object.items.id[0].toString()


def ItemCountResponse = context.expand( '${Get Funding Frequencies#Response#$[\'itemCount\']}' )
log.info ItemCountResponse.toString()

def int i;

for(i=0; i<=ItemCountResponse; i++)
{
if(i<ItemCountResponse)
{
//testRunner.runTestStepByName("Get Funding Frequencies")
log.info i
}
else
{
End
}
}

 

Can anyone guide me through this error, also if you can confirm if the For Loop Statement is correct in my script that would be great. 

  • Do you have the whole error message or know what line the error occured on? I have not been able to try it but by guess is that the error is coming from i<=ItemCountResponse in the for loop. My guess is that ItemCountResponse is not an numeric value. What value do you get if you output ItemCountResponse without the .toString() ?

     

    Also, you dont need to define the variable i before you use it in the for loop. The variable is declared when you do for(i=0;

    • sonya_m's avatar
      sonya_m
      SmartBear Alumni (Retired)

      Thank you jsheph01! 

       

      MohammedRehman, please provide the info jsheph01 is requiring, we might find a solution for you! Thanks.