Forum Discussion

cdunne's avatar
cdunne
Contributor
17 years ago

Groovy Error - java.lang.String exception thrown

Hi,

I'm getting a java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.String exception thrown in a groovy step.

I have a test case property which holds a count variable for use every time I loop through a template test case. My groovy step is to be used to call some custom reporting functionality.

def currentTest = context.expand( '${ReferenceData_DataSource_Request#Test_Name}' )

// read the "ReportCount" property
def count = testRunner.testCase.properties["ReportCount"].value
log.info(count)

// read all the previous test step results up to the current test step
def results = testRunner.results
log.info("Reading "+results.size()+" previous test step results...")

// loop through the results and check for falures
// if it is the first loop in the template test will start at position 0 of results, else start
// at the updated count position.
while(count if(results[count].status.toString() == "FAILED"){
break
count++
}
}

// if any of steps fails - log some error condition otherwise report test case passed
if(count log.info(currentTest+" : Failure in TestStep ["+results[count].testStep.name+"]")

for(msg in results[count].messages){
log.info(msg)
}
}
else
{
log.info(currentTest+": PASSED")
}

// set the "ReportCount" property for use in the next loop of the template test case
log.info("writing count value to ReportCount: "+results.size())
testRunner.testCase.properties["ReportCount"].value = results.size()

The folloing output is produced in the groovy log:-
Tue Apr 22 10:05:01 BST 2008:INFO:0
Tue Apr 22 10:05:01 BST 2008:INFO:Reading 3 previous test step results...
Tue Apr 22 10:05:01 BST 2008:INFO:AccountAdjustmentStatus: PASSED
Tue Apr 22 10:05:01 BST 2008:INFO:writing count value to ReportCount: 3
Tue Apr 22 10:05:01 BST 2008:INFO:3
Tue Apr 22 10:05:01 BST 2008:INFO:Reading 7 previous test step results...
Tue Apr 22 10:05:01 BST 2008:INFO:AccountType: PASSED
Tue Apr 22 10:05:01 BST 2008:INFO:writing count value to ReportCount: 7
Tue Apr 22 10:05:01 BST 2008:INFO:7
Tue Apr 22 10:05:01 BST 2008:INFO:Reading 11 previous test step results...
Tue Apr 22 10:05:01 BST 2008:INFO:AdjustmentType: PASSED
Tue Apr 22 10:05:01 BST 2008:INFO:writing count value to ReportCount: 11
Tue Apr 22 10:05:02 BST 2008:INFO:11
Tue Apr 22 10:05:02 BST 2008:INFO:Reading 15 previous test step results...
Tue Apr 22 10:05:02 BST 2008:ERROR:java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.String
Tue Apr 22 10:05:02 BST 2008:INFO:11
Tue Apr 22 10:05:02 BST 2008:INFO:Reading 19 previous test step results...
Tue Apr 22 10:05:02 BST 2008:ERROR:java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.String

The script appears to work for the first 3 loops but then throws the Exception. Anybody got any suggestions?

Cheers

Cormac
  • omatzura's avatar
    omatzura
    Super Contributor
    Hi,

    please try replacing

    def count = testRunner.testCase.properties["ReportCount"].value

    with

    def count = Integer.parseInt( testRunner.testCase.properties["ReportCount"].value )

    regards,

    /Ole
    eviware.com
  • Hi Ole,

    Thanks for the response.

    The error was a bit misleading because the first 3 loops of the script passed and then the last 2 threw the exception. I would assume that the 1st time the script is called it should fail?

    Thanks

    Cormac
  • omatzura's avatar
    omatzura
    Super Contributor
    Hi Cormac,

    sorry.. well, you never know.. so making sure it's a Integer might be a good idea anyhow.. but I can't see anything else that could be causing this, could you please try to see if it helps!?

    regards!

    /Ole
    eviware.com