How to Write Basic Groovy Script in SoapUI – SoapUi Tutorial #6

In this SoapUI tutorial we will learn basic scripting operations using the Groovy script and following that, we will learn different types of operators that can be used in Groovy scripts in SoapUI.

Let’s begin with groovy scripting and how we can use it in SoapUI Pro. This is the 6th tutorial in our SoapUI free online training series.

Groovy Script in SoapUI

SoapUI Groovy Script Test step:

Groovy Script test step is included for custom automation test script creation in SoapUI / Pro. It can be used for functional/ load/regression.

Groovy is a scripting language which internally includes all the Java libraries, therefore all java related keywords and functions can be used in the groovy script directly. The Java libraries come with SoapUI and are integrated during the SoapUI Pro installation itself.

Here is how Groovy script can be added to a test:

Step #1. In SoapUI Pro create a SOAP project with valid WSDL document. Under the project, create a test suite with the desired name. Inside the test suite, add groovy script test step as shown below:

Working with Operators 1

Step #2. Enter the name of the step in the dialogue that comes up as below and Clicks OK

Working with Operators 2

Step #3. An editor where you can write your script is displayed. Alternately you can double click on the groovy step name from your test case (Groovy step is the one that has a star prefix to it).

(Click on image for enlarged view)

Working with operators 66

For example: Let us write a simple script that shows a message in the log. Here is the one line script. ”soapUI script”

Step #4. To execute the above script in SoapUI Pro, click on the Run icon and see the results in the Log Output section.

Working with operators 44

Few points:

  • Test script execution: When the run button inside the groovy editor is clicked, the code inside the groovy step will only get executed. On the other hand, when the Run button is clicked for the entire test case, all the steps are executed in an order.
  • This way any kind of programming can be done to the test scripts to add validations as required.
  • There can be any number of groovy test steps to a test case.
  • With a Groovy script, it is not required to compile and interpret separately to execute the code like other programming languages such as C, C++, Java, etc.
  • Steps can be enabled or disabled inside a test suite by using the comment feature. To do so, use the following:

                 // – indicates single line comment and
                /* <some script> */ – denotes multi-line comment

Arithmetic Operations:

In the groovy step editor all of the below can be performed:

/* Adding Two numbers */
int a;
int b;
int c;

// Assigning integer value to the variables A and B
a = 100;
b = 200;

// Adding A value and B value and assign the resultant value to the variable C
c = a + b

// Show the resultant value in the Log“Result :” + c);

In the above script, A, B and C are the variables which are used to store or transfer the values.

(Click on image for enlarged view)

Working with operators 55

Note: Variables in the Groovy script are case sensitive. Exercise caution when using them.

The following are the operators supported in Groovy:

Arithmetic Operators:

   Addition operator / String concatenation
   Subtraction operator
   Multiplication operator
   Division operator
%   Remainder operator

// Arithmetic Operators Samples

// Addition Operator
int x1 = 100 + 200 (“Addition Result :” + x1);

// Concatenation of Two Strings using PLUS ( + ) operator
String city =”Timothy E.” + ” Shepherd”;“String Concatenation:” + city);

// Subtraction Operator
int x2 = 200 – 100 (“Subtraction :” + x2);

// Multiplication Operator
int x3 = 10 * 200 (“Multiplication :” + x3);

// Division Operator
int x4 = 200 / 10 (“Division :” + x4);

// Modulus Operator
int x5 = 10 % 3 (“Reminder or Modulus:” + x5);

The following is a screenshot of all of the above scripts and the respective results:

Working with Operators 6

Unary Operators:

Unary operators are the ones that work with only one operand. For example: ++ – it is called as Increment operator which increments the current value by 1

Here’s the example:

int A = 100;
A++;                     // Equivalent to A = A + 1 (A);

The above script will produce the output as 101. This increment operation is called post increment. Similarly we can use this operator as a pre-increment operation as below:

int A = 100; (++A);

There is also (–) the decrement operator. It will decrease the current value by 1. We can implement this operator to the above discussed examples.

int A = 100;
A–;                       // Equivalent to A = A – 1 (A);

The above script will produce the following output:
Mon Jul 21 18:02:16 IST 2014:INFO:99

The pre and post operations can be used with decrement operator as well.

Assignment Operators:

The basic assignment operator is an equal sign (=). Likewise, there are other useful assignment operators available. They are +=, -=, *=, /=, %=.

Let us see the samples.

int A=100;
A += 10;                // Similar to A = A + 10;

The above script produces 110. If we use minus equal to operator in the below script, output will be 40.

int B=50;
B -= 10;;

Likewise we can use the remaining operators like this.

int C=10;
C *= 10;;


int D=50;
D /= 10;;

Here’s the reminder operator is used as

int E=10;
E %= 3;;

This will divide the value 10 by 3 and the remainder will be assigned to the variable “E”.


This is just a start and there are many other operators available and supported by groovy such as logical, comparison, conditional etc. which will be discussed in the upcoming tutorials. In next SoapUI tutorial, we will also learn how to deal with properties in SoapUI Groovy scripts.

Do come along with us and please post your comments and questions for us below.

Recommended Reading

23 thoughts on “How to Write Basic Groovy Script in SoapUI – SoapUi Tutorial #6”

  1. Thanks for making things so easy to understand,

    Groovy is looking quite similar to other languages only instead of print/printf/System.println
    here we have;

  2. Executing this groovy script throws error like

    org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed: Script11.groovy: 14: Invalid variable name. Must start with a letter but was: ” . At [14:20] @ line 14, column 20. (“Result :” + c); ^ org.codehaus.groovy.syntax.SyntaxException: Invalid variable name. Must start with a letter but was: ” . At [14:20] @ line 14, column 20. at org.codehaus.groovy.control.CompilationUnit$ at org.codehaus.groovy.control.CompilationUnit.applyToPrimaryClassNodes( at org.codehaus.groovy.control.CompilationUnit.doPhaseOperation( at org.codehaus.groovy.control.CompilationUnit.processPhaseOperations( at org.codehaus.groovy.control.CompilationUnit.compile( at groovy.lang.GroovyClassLoader.doParseClass( at groovy.lang.GroovyClassLoader.parseClass( at groovy.lang.GroovyShell.parseClass( at groovy.lang.GroovyShell.parse( at groovy.lang.GroovyShell.parse( at groovy.lang.GroovyShell.parse( at at at at com.eviware.soapui.impl.wsdl.panels.teststeps.GroovyScriptStepDesktopPanel$RunAction$ at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source) at java.util.concurrent.ThreadPoolExecutor$ Source) at Source) 1 error”

    pls help me… what to do here..

    /* Adding Two numbers */
    int a;
    int b;
    int c;

    // Assigning integer value to the variables A and B
    a = 100;
    b = 200;

    // Adding A value and B value and assign the resultant value to the variable C
    c = a + b

    // Show the resultant value in the Log“Result :” + c);

  3. I have a JDBC step defined in my soapui script , i want to extract a value like this value from the query response , and assign it to a project variable , is that possible from groovy scripting ?

  4. The groovy script errors are due to formatting issues in the example scripts. Re-enter the script quote (“) characters manually. Some of the hyphens (-) need to be re-entered manually as well.

  5. pls help me guys
    step 1. generate random number in groovy script step 2. connect to database step 3. verify whether generated random number exist in database or not(check in all rows) if random number exist stop execution else continue execution

  6. @Praveen: in your case you need to put the string ‘Result’ in single quote and not in double quote.

    tested code- ‘soapUI script’

    int a;
    int b;
    int c;


    c=a+b;‘Result: ‘+c);

  7. Hi

    I needed help with a task…I have 8 restful service calls .I need to test them in a sequence.
    I need to pass the output of preceding restful service as input to the next service.
    I have to use SOAP UI and maybe Groovy Scripting / Java
    Pls let me know if you can help me.Pls let me know if you need more details.

  8. Hai All..
    I am a Beginner in Soap UI
    I Want to Know information about groovy script for JSon model
    Here example are for XML model
    how to write groovy script for Request and Resonse in Json Model

    Pls any one help Me fix this…….

  9. I want to extract data from excel file that is saved anywhere in directory using groovy script, Can anyone help me?

  10. @sonya,

    It’s possible to get the value from JDBC response and assign it to a project variable. Refer the following script.

    query = “SELECT FROM < WHERE ID = 103231”

    tc = projectName.testSuites[‘TestSuite’].testCases[‘MyTestcase’]
    tstp = tc.getTestStepByName(‘TestStep’)
    runner =,context)

    responseHolder = runner.getResponseContent()
    runner = null

    resultXml = new XmlSlurper().parseText( responseHolder )
    relObjIds = resultXml?.ResultSet?.Row?.collect{it.<>}
    relObjectId = relObjIds[0]

  11. Adding my comment..

    query = “select column_name from table_name where id = 103231

    relObjIds = resultXml?.ResultSet?.Row?.collect{it.column_name}

  12. @Amit Pratap,

    To read data from Excel sheet from Groovy, you need to use either JXL.jar or POI.jar file and this file should be kept in SoapUI installation path \ext folder.

    And write script using java code.

  13. How Can I read value from a property in xml request

    if i want to read the value of id and send response based on it in mock service, how can i do that


Leave a Comment