cancel
Showing results for 
Search instead for 
Did you mean: 

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

SR1
New Contributor

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?

Tags (2)
3 REPLIES 3
Community Hero

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

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
New Contributor

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

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. 

Community Hero

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

Should work.

New Here?
Join us and watch the welcome video:
Join the exciting event
SeptemberHubBub
Top Kudoed Authors
Join the September Hub-bub to show off, learn and win