Forum Discussion

SR1's avatar
Occasional Contributor
5 years ago

List set in Context.setProperty of testSuite modifiable in testCase context but not string,int etc

In soapUI, I have a List in testSuite which I setup using context.setProperty is visible and modifiable in testcase1 and testcase2. I'm able to modify the list in both testcases, but I'm not able to do the same for strings or integers or floats etc. Please see the below code and guide me accordingly. I know that we can use propertyValue to set/change string, but I don't want to use that as my string size is very huge and i have array of such strings.


/*soapUI groovy code:
List l=[]
String str="suite"

//In TESTCASE1:"From TC1")
for(lst in arrlst){}
arrlst.add("First added in TC1")
for(lst in arrlst){}   //prints "First added in TC1"

str=context.getProperty("string")"The string is"+context.getProperty("string"))  //prints "the string is suite"
str="This is TC1!""The string is"+context.getProperty("string")) //still prints "the string is suite"
context.setProperty("string",str)"The string is"+context.getProperty("string")) //prints "the string is this is TC2" but its in TC1's local context

//In TESTCASE2:"From TC2")
for(lst in l1){} //prints "First added in TC1"
str=context.getProperty("string") //prints "the string is suite" not the value set in the testcase1

 why im able to change/modify the list in other testcases but not the strings, integers etc?

4 Replies

  • JHunt's avatar
    Community Hero

    This is because each TestCase has its own TestCaseRunContext, and so your TESTCASE1 is not looking at the same list as TESTCASE2.


    When a TestSuiteRunner kicks off a TestCase, it copies the entries in the TestSuiteRunContext to a new TestCaseRunContext to run the test. So both test cases are looking at copies of the original values from the TestSuiteRunContext.


    In the case of the List, a reference to an instance of a List is copied, so its the same object. But for primitive types, the value is copied.


    You can access the TestSuiteRunContext directly instead, so that you having the same objects. Like this:

    def testSuiteContext = context.get("#TestSuiteRunner#").getRunContext()

    Just remember that this won't exist if you run your Test Case or Test Step by itself instead of by running the Test Suite.

    • SR1's avatar
      Occasional Contributor

      Thanks for your reply Sir. Good answer. So If i want to share&update the primitive types between testcases, can i  have the primitive values as members in a class and create a object in testsuite context and update the members in different testcases? will it work? object will be reference copied or value copied? please advice.