Enterprise Integration Zone is brought to you in partnership with:

Meera has posted 70 posts at DZone. You can read more from them at their website. View Full User Profile

Functional Web Services Testing Made Easy with SoapUI - Part 2

  • submit to reddit
Part 1 of this series helped provide the background needed to begin exploring web services testing. We learned the basics of SoapUI and how easy it was to write functional tests without writing a single line of code. We also saw how to add assertions to these tests. What we will examine now is how to use Groovy within SoapUI for test setup, test teardown, response validation, and much, much more.

Before we begin, a quick Groovy primer. We start with the fact that Groovy is Java and Java is Groovy. If you have written Java code, you have written Groovy code. Our tasks within this article will be to do 3 things: format a date, read and write to a properties file, and parse XML. Let’s look at the Groovy code for each of these in detail.

1. Format a date
The standard Java class for this task is SimpleDateFormat. This is a concrete subclass of DateFormat formats and parses dates and times using a string pattern, in our case, "yyyy-MM-dd". Let’s start by writing this in Java; later we will see how easily we can groovify this to use within SoapUI. Here is the Java code to get the date in the format “yyyy-MM-dd”.

java.util.Date today = new java.util.Date();
java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat("yyyy-MM-dd");
String todayStr = sdf.format(today);

We could leave the Groovy code exactly as it is above, but let’s make it groovier, removing the bells and whistles necessary in Java. Our code finally looks like this:

today = new Date()
sdf = new java.text.SimpleDateFormat("yyyy-MM-dd")
todayStr = sdf.format(today)

Not a lot of change, but considerably faster to type. The similarities to Java are just as we have said.

2. Reading and writing to a properties file

The java.util.Properties object does the work of reading and writing to a properties file in Java. This time we’ll go directly to the Groovy code:

sdf = new java.text.SimpleDateFormat("yyyy-MM-dd")
todayStr = sdf.format(new Date())
props = new java.util.Properties ()
file = new File("C:/web-services-test/testprops.txt")
props.today = todayStr
props.zipCode = "20904"
fos = new java.io.FileOutputStream ( file )
props.store(fos, "Writing the zipcode and today's date")
}fis = new FileInputStream (file )
props.load (fis)
today = props.getProperty ( "today" )
zipCode = props.getProperty ( "zipCode" ) 

Simple, right? Similarly, if you need to read from a database, you can do so in a few simple lines of Groovy code.

3.Parse XML

If you have been writing enterprise Java applications you have certainly needed to use XML, and you can testify that XML and Java is not easy. Parsing XML in Java requires a lot of boilerplate code and is really tedious.

You may remember from part 1 that I said I was lazy. Fortunately, Groovy comes to the rescue. We can parse XML in just 5 lines of code. Yes, that’s what I said, just 5 lines. Let’s see how to parse the response from the LatLonListZipCode web service from Part 1. The XML looks like this:

<SOAP-ENV:Envelope SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/">
<ns1:LatLonListZipCodeResponse xmlns:ns1="http://www.weather.gov/forecasts/xml/DWMLgen/wsdl/ndfdXML.wsdl">
<listLatLonOut xsi:type="xsd:string"><?xml version='1.0' ?>
<dwml version='1.0' xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://www.nws.noaa.gov/mdl/survey/pgb_survey/dev/DWMLgen/schema/DWML.xsd">

The code to parse this in Groovy is so easy:

groovyUtils = new com.eviware.soapui.support.GroovyUtils( context )
holder = groovyUtils.getXmlHolder("LatLonListZipCode - Request 1#Response")
listLatLonOut = holder.getNodeValue( "//listLatLonOut" )
latlonNode = groovyUtils.getXmlHolder(listLatLonOut)
latlon = latlonNode.getNodeValue("//latLonList")

Article Resources: 
Published at DZone with permission of its author, Meera Subbarao.

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)


John Baker replied on Wed, 2008/11/19 - 3:20am

Thanks for the great article, Meera! I had figured out the basics of soapUI for testing simple requests, but I needed a way to propagate a larger number of test cases with various combinations of optional reqeust elements. Following you example I now have the tools I need to get moving. I also feel motivated to learn more about Groovy. Thanks again!

Meera Subbarao replied on Wed, 2008/11/19 - 8:05am

I am glad this article helped. Yes, there are numerous ways you can use Groovy, not just with SoapUI, and I am glad I learnt this.

Ehsan Sadeghi replied on Fri, 2009/01/16 - 6:26am


one question, is it possible to pass the responce from one test case to another test case. What i'm trying to do is to get a session id and then use that session id to run a load test.


Meera Subbarao replied on Fri, 2009/01/16 - 10:58am in response to: Ehsan Sadeghi

Yes, it is possible. See part 2 which shows exactly how to do this.

shanthi bala replied on Fri, 2009/02/27 - 2:25am

Hi Meera Can I write tests within SOAP UI and invoke it in a seperate Java program ? Also I need to parse the result in this Java code. Thanks Shanthi

Meera Subbarao replied on Tue, 2009/03/03 - 11:18am in response to: shanthi bala

Yes, you can do that. You can easily do that.

Suresh Reddy replied on Wed, 2009/03/18 - 10:24pm

After seeing into ur article I whould like to know how can i obtain session ID to logout automatically when I login and logout of the webapplicaiton.

Nelson Nazareth replied on Tue, 2009/06/16 - 6:47pm

Hi Meera, Thanx for the wonderful tutorial. I am using soapUI as a mock webservice server. I have tried the various options currently available and they are good. I have one need. Using groovy I want to evaluate the incoming request and based on some condition would like to return response from a list of available responses. Please remember this needs to done from the script itself. Can you please provide an approach?

syed ali replied on Thu, 2009/08/06 - 4:25am

Hi Meera,

How could I change the project properties, test suite properties or testcase properties values using Groovy script test step. I wasn't able to understand the syntax.

def sessionID = context.expand( '${#Project#SessionID}' )
def testSuiteProperty = context.expand( '${#TestSuite#TestSuiteProperty}' )
def test1 = context.expand('${#TestCase#test}' )

I understood the above lines collect the values of the given properties. Please help me to set the value for  SessionID, TestSuiteProperty, test parameters. Thanks in advance.



syed ali replied on Fri, 2009/08/07 - 2:29am in response to: syed ali

I have found the info. The format is as follows:

testRunner.testCase.setPropertyValue("test","testcase property")

However, I have another issue. If the assertion is faliled I am unable to continue to execute the further steps of testcase. Please help me regarding this concern...

kiran kmk replied on Mon, 2009/09/14 - 4:16am


I am trying to get the Node Value but I get it as Null

IST 2009:INFO:null

This is the Script I am running groovyUtils = new com.eviware.soapui.support.GroovyUtils( context ) holder = groovyUtils.getXmlHolder("GetData - Request 1#Response") dwmlByDayOut = holder.getNodeValue( "//GetDataResponse" )

Below are the Request Iam sending and the response I get


This is my response

You entered: 500

Geza Szent-andrassy replied on Fri, 2009/10/09 - 3:13am

Hi all!

For the TestCase setup script to work, i think it needs to add the following lines before file.createNewFile(), Otherwise i experienced an IOException, which was thrown by file.createNewFile(), when the parent directory had not existed before.


    if (file.getParentFile() != null) {


 Nice article, anyway!


john green green replied on Mon, 2009/10/26 - 3:26am

I am glad this article helped. Yes, there are nike shoes russia numerous ways you can use Groovy, not just with SoapUI, and I am glad I learnt this.

Naveen Kumar replied on Wed, 2009/11/25 - 7:31am

Hi All, I am new to the SOAP UI tool, I want to write the results in text file in the format of testsuitename,testcasename,test case start time,testcase end time by using groovy script. Please let me known if any one have any frame work explaing abt this format.

Rambabu Nadakudati replied on Fri, 2010/03/05 - 12:37pm

Hi Meera, I am having an issue with the Global Properties (Test Suite level) in SOAP UI. Below is the use case I am trying to achieve. 1. I have to execute total 3 services under one test suite. For this, created a separate test case for each service (under one test suite). 2. The 2nd service requires first service response data & the 3rd service requires 2nd service response. For this, I am trying to store the data (such as sessionid) in Global properties (Test Suite level) by using below code: testRunner.testCase.testSuite.project.setPropertyValue("SessionID", "XYZ"); Question: When I store the property value with the above code, I am not able to see the storing value in the property window of the test suite. Please advice.

Rambabu Nadakudati replied on Tue, 2010/03/09 - 3:32pm

Found the fix for my issue. You should use the below code: testRunner.testCase.testSuite.setPropertyValue("SessionID", "XYZ");

Nikhil Gupta replied on Fri, 2010/07/23 - 6:09am

Hi , This is a greate blog. I am using SOAP UI and have configured the testcase request information in the property file as you have suggested in the blog. Now the problem is that , the property values are getting hardcoded in the SOAPUI Project XML in the tag. The requirement is since i have configured the values using property file i should be able to change it from the property file without using the SOAP UI interface. Is there a way to that. SOAP UI version : 2.0.2 Please provide your valuable comments. Please do let me know in case you require more details. Thanks In Advance Nikhil Gupta nikgupta@gmail.com

Test Dude replied on Wed, 2010/10/20 - 3:17pm

Hi, i am trying to read a xml file content and store it in result property of groovy script like below new File("H:/Soujanya/Folder").eachFile() {file -> //def props = new java.util.Properties () def tc = testRunner.testCase tc.setPropertyValue( "result",(file.getText("ISO-8859-1"))) } But the xml content is not storing in result property..it is just resulted in null..please let me know if i am doing something wrong asap. Thanks in advance.

Durga Nuvvula replied on Tue, 2010/12/07 - 5:49pm

My objective is to get the response values out of the Response that is obtained from the SOAP UI in the assertion script.

I have tried to parse the response the way you suggested in the article and its returning null.

def groovyUtils = new com.eviware.soapui.support.GroovyUtils( context )

def responseHolder = groovyUtils.getXmlHolder("get-xyz-response Request 1#Response" )

 when trying to get the


def appearanceColorResponse = responseHolder.getNodeValue("//appeareance-color")

I am getting the appearanceColorResponse as null.

Also I tried the other way registering the namespaces before doing the xpath.

def groovyUtils = new com.eviware.soapui.support.GroovyUtils( context )
def responseHolder = groovyUtils.getXmlHolder( messageExchange.responseContent )

declare namespace ns4='http://www.xyz.com/schemas/xyz/abc';
declare namespace ns1='http://www.xyz.com/schemas/xyz/def';
declare namespace ns2='http://www.xyz.com/schemas/xyz/klm';
declare namespace ns3='http://www.xyz.com/schemas/xyz/pqr';

def appearanceColorResponse = responseHolder.getNodeValue("//ns2:message[1]/ns2:message-body[1]/ns1:person-detail[1]/ns3:person[1]/ns4:entity[1]/ns4:appeareance-color[1]);


I am getting the appearanceColorResponse as null.

Ram Kumar replied on Tue, 2011/04/19 - 9:35am

Hi meera,


How do i do data driven testing using excel sheets in soap ui along with groovy.. i mean reading data through an excel sheet. and i would like to capture the result from soap response and to write in to the specific cell in the out put excel document.


Please give me the details..I need to write some regression suites ..






Mahadeva Prasad replied on Wed, 2011/06/15 - 6:01am


I am new to SOAP UI.I wrote my application in using cakephp framework.I want to conduct web testing using a SOAP UI,like how to click a links and how to click a submit button once all input fields are entered.And also i want information about WSDL files.

Sandeep Manganellore replied on Fri, 2011/10/14 - 10:31am in response to: Meera Subbarao

HI I have Question, I am new to Groovy I automating the application using soapUI, I am trying load the xmls from groovy and post it to an endpoint (queue). can you help me how to write code for this. else please let me know change request text of a 'Test Request' in soapui using groovy and run it?

Vivek Kumar replied on Fri, 2011/12/30 - 10:39am

Hi Meera, I am new to SOAPUI and going through various web tutorials and contents to understand how to write groovy script to control web services testing.I liked your website and learnt a lot about soapui and groovy.I have used QTP extensively for web services testing but have very limited knowledge of SOAPUI and Groovy and I am more into VB Scripting and not much into Java though I understand its syntax and logics.Please help me by providing code to explain data driven testing using groovy in soapui.I have free trial version of soapui pro and I do not want to use datasource a feature of sopaui which is available only in paid version of soapui.Is there any way I can write groovy script to read and write to external excel spreadsheet that will resolve my question.Please help me as soon as possible.Thanking in advance. Regards Vivek

Carla Brian replied on Sun, 2012/07/15 - 5:36pm

After determining the requirements for functional testing of Web services, it was necessary to examine some of the readily available test tools. - Mercy Ministries

Peter Keller replied on Fri, 2012/09/14 - 3:00pm

Loading the WSDL you used I got following error message:

"Error loading [http://www.weather.gov/forecasts/xml/DWMLgen/wsdl/ndfdXML.wsdl]: org.apache.xmlbeans.XmlException: org.apache.xmlbeans.XmlException: error: does not close tag"

So I used following URL instead: http://graphical.weather.gov/xml/SOAP_server/ndfdXMLserver.php?wsdl

However, invoking the ZIP service using this URL, I get following response where the latLonList is not a proper XML tag but a escaped text field:

<SOAP-ENV:Envelope SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/">
      <ns1:LatLonListZipCodeResponse xmlns:ns1="http://graphical.weather.gov/xml/DWMLgen/wsdl/ndfdXML.wsdl">
         <listLatLonOut xsi:type="xsd:string">&lt;?xml version='1.0'?>&lt;dwml version='1.0' xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xsi:noNamespaceSchemaLocation='http://graphical.weather.gov/xml/DWMLgen/schema/DWML.xsd'>&lt;latLonList>38.8787,-77.1019&lt;/latLonList>&lt;/dwml></listLatLonOut>

Am I doing something wrong? Does your setup still work? I am using SoapUI 4.5.1. Thanks.




Andri Purmawinata replied on Thu, 2013/04/04 - 12:55am


Thanks very much for your blog.  Sorry for newbie question.  I'm new to api/web services groovy testing.

What does it mean with ("LatLonListZipCode - Request 1#Response")?

holder = groovyUtils.getXmlHolder("LatLonListZipCode - Request 1#Response")
It's referring to this line? <ns1:LatLonListZipCodeResponsexmlns:ns1="http://www.weather.gov/forecasts/xml/DWMLgen/wsdl/ndfdXML.wsdl">I'm a bit confused with: "LatLonListZipCode - Request 1" bit.
Can you please explain:
holder = groovyUtils.getXmlHolder("LatLonListZipCode - Request 1#Response")
Thanks very much!

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.