Forum Discussion

SR1's avatar
SR1
Occasional Contributor
6 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:
//In TESTSUITE:
List l=[]
context.setProperty("list",l)
String str="suite"
context.setProperty("string",str)

//In TESTCASE1:
log.info("From TC1")
arrlst=context.getProperty("list")
for(lst in arrlst){
log.info(lst)}
arrlst.add("First added in TC1")
for(lst in arrlst){
log.info(lst)}   //prints "First added in TC1"

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

//In TESTCASE2:
log.info("From TC2")
l1=context.getProperty("list")
for(lst in l1){
log.info(lst)} //prints "First added in TC1"
str=context.getProperty("string")
log.info(str) //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?

  • JHunt's avatar
    JHunt
    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
      SR1
      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.