Forum Discussion

pkinoc's avatar
pkinoc
Occasional Contributor
14 years ago

Shared script across multiple Test Cases

I have a groovy script with a synchronized method that does the following:

Read a number out of a txt file
Increment it by 1
Update the txt file with the new number


Now, I have multiple Test Cases that need to run this static method. How do I accomplish this? The following is what I tried

If I create a class with a static synchronized method within one of my Test Case Groovy scripts and have that script call that method further down in the script then it works for one test case.
result = SynchronizedCode.GetNewOrderNumber() -
import groovy.transform.Synchronized
class SynchronizedCode {
@Synchronized
static String GetNewOrderNumber() {
def input = new File("C:\\Impact_WESB\\OrderID.txt")
def orderID = input.text
println "Last OrderID= " + orderID;
orderID = orderID.replaceAll("OrderID=", "")
println "OrderID= " + orderID;
def OrderIDInt = orderID.toInteger()
OrderIDInt++
println "New OrderID= " + OrderIDInt;
File f = new File("C:\\Impact_WESB\\OrderID.txt")
f.write("OrderID=" + OrderIDInt)

return OrderIDInt
}

}


Now if I move the class above to the TestSuite Setup script section so that the class can be imported into the Soapui process and then have each Test Case groovy script call the static method it complains

grrovy.lang.MissinPropertyException: No such property SynchronizedCode for class Script16 ....

So obviously it can't find the class. Is there a way to resolve this so that all groovy scripts can see the SynchronizedCode.GetNewOrderNumber() method?

TestSuite Setup Script
----------------------
import groovy.transform.Synchronized

class SynchronizedCode {
@Synchronized
static String GetNewOrderNumber() {
def input = new File("C:\\Impact_WESB\\OrderID.txt")
def orderID = input.text
println "Last OrderID= " + orderID;
orderID = orderID.replaceAll("OrderID=", "")
println "OrderID= " + orderID;
def OrderIDInt = orderID.toInteger()
OrderIDInt++
println "New OrderID= " + OrderIDInt;
File f = new File("C:\\Impact_WESB\\OrderID.txt")
f.write("OrderID=" + OrderIDInt)

return OrderIDInt
}

public static void main(String[] args) {
SynchronizedCode.GetNewOrderNumber() // Initialize the class so all testcases can see it
}
}

1 Reply

  • RJanecek's avatar
    RJanecek
    Regular Contributor
    you can create groovy library and then add this jar to bin/ext. Then you can call this static method from groovy.

    import your project

    NameOfYourProject.MethodName()