Forum Discussion

thedivxboy's avatar
thedivxboy
Occasional Contributor
16 years ago

ClassCastException - getProperty() used with array

Hello,

In a groovy script step I have this code:

def sides = ["value1", "value2"];
context.setProperty("sides", sides);


And in a listener (java code complied to a jar) I want to access each element of my array. But I don't know the correct syntax.

I tried this without success:

String[] sides = (String[]) runContext.getProperty("sides");


This code causes the following error in the testcase log:

TestCase failed [java.lang.ClassCastException: java.util.ArrayList:java.lang.ClassCastException: java.util.ArraytList], time taken = 28387


The solution is probably easy but I'm not familiar with SoapUI casting and I've not found any specific method for array properties in the API.

Thanks in advance,
Arnaud
  • Hi,

    I've found the problem .. It was a very strange problem ...
    I've simply inversed the two following lines:

    List sides = (List) runContext.getProperty("sides");
    String senderSideStatus, receiverSideStatus;


    I suppose it's a bug in SoapUI or Java because it's not very logic I think

    Bye,
    Arnaud.
  • thedivxboy's avatar
    thedivxboy
    Occasional Contributor
    Hi,

    I've found the problem .. It was a very strange problem ...
    I've simply inversed the two following lines:

    List sides = (List) runContext.getProperty("sides");
    String senderSideStatus, receiverSideStatus;


    I suppose it's a bug in SoapUI or Java because it's not very logic I think

    Bye,
    Arnaud.
  • omatzura's avatar
    omatzura
    Super Contributor
    Hi!

    the object returned is a java.util.List, try using that type instead!

    regards,

    /Ole
    eviware.com
  • thedivxboy's avatar
    thedivxboy
    Occasional Contributor
    Now I use
    List<String> sides = (List<String>) runContext.getProperty("sides");
    String side1 = (String) sides.get(0);
    String side2 = (String) sides.get(1);


    But SoapUi doesn't launch. It shows the following error in the command prompt:
    Exception in thread "main" java.lang.ClassFormatError: Field "side1" in class soapui/testStep/TestStepListener has illegal signature "append" ...


    I really don't understand the problem. This code causes no error in Eclipse ..


    Arnaud
  • omatzura's avatar
    omatzura
    Super Contributor
    Hi,

    strange! Try doing this without generics? ie

    List sides = (List) runContext.getProperty("sides");
    String side1 = (String) sides.get(0);
    String side2 = (String) sides.get(1);

    How are you compiling this class?

    regards,

    /Ole
    eviware.com
  • thedivxboy's avatar
    thedivxboy
    Occasional Contributor
    Hi,

    Your method doesn't work ..
    I compile this class normally I think. Without these lines my script works ...

    Arnaud.
  • omatzura's avatar
    omatzura
    Super Contributor
    Hi,

    I'm sorry, I have no idea what could be causing this.. can you show your entire class?

    regards,

    /Ole
    eviware.com
  • thedivxboy's avatar
    thedivxboy
    Occasional Contributor
    Hi,

    Here my class:

    package soapui.testStep;

    import java.util.List;

    import com.eviware.soapui.model.testsuite.TestRunContext;
    import com.eviware.soapui.model.testsuite.TestRunListener;
    import com.eviware.soapui.model.testsuite.TestRunner;
    import com.eviware.soapui.model.testsuite.TestStepResult;
    import com.eviware.soapui.model.testsuite.TestStepResult.TestStepStatus;

    public class TestStepListener implements TestRunListener {
        public void beforeRun(TestRunner testRunner, TestRunContext runContext) { }

        public void afterRun(TestRunner testRunner, TestRunContext runContext) { }

        public void beforeStep(TestRunner testRunner, TestRunContext runContext) { }

        public void afterStep(TestRunner testRunner, TestRunContext runContext, TestStepResult result) {
            if (result.getStatus()==TestStepStatus.FAILED) {
    //            List sides = (List) runContext.getProperty("sides");
                String senderSideStatus, receiverSideStatus;

    //            senderSideStatus  = (String) runContext.getProperty("expected" + (String) sides.get(0) + "ModelStatus");
    //            receiverSideStatus = (String) runContext.getProperty("expected" + (String) sides.get(1) + "ModelStatus");
                senderSideStatus  = (String) runContext.getProperty("expected" + (String) runContext.getProperty("side0") + "ModelStatus");
                receiverSideStatus = (String) runContext.getProperty("expected" + (String) runContext.getProperty("side1") + "ModelStatus");


                if (runContext.getCurrentStep().getName().equals("receiveRequest") && (senderSideStatus.equals("Failed") || (senderSideStatus.equals("Started") && receiverSideStatus.equals("Failed")))) {
                    // Test must fail so it is OK
                } else {
                    runContext.setProperty("testFailed", true);
                    runContext.setProperty("testStepName", runContext.getCurrentStep().getName());
                    runContext.setProperty("testStepErrors", testRunner.getResults().get(runContext.getCurrentStepIndex()).getMessages());
                }

                testRunner.gotoStepByName("Loop to next properties file");
            }
        }
    }


    I hope this will help you to help me
  • omatzura's avatar
    omatzura
    Super Contributor
    Hi,

    strange indeed.. good to hear that you solved it..

    regards!

    /Ole
    eviware.com