This page discusses how to test Web Services using Groovy directly and in conjunction with WebTest and SoapUI.
Testing Web Services can be done in several ways. Here are three:
- act like a normal web services client and perform asserts on the returned result
- use WebTest (with either the XML or Groovy syntax)
- use SoapUI (for functional and load testing)
We are going to use the Web Service example at:
http://groovy.codehaus.org/Groovy+SOAP
Being a client
You can be a normal client web service client and perform asserts on the returned results:
import groovy.net.soap.SoapClient def proxy = new SoapClient("http://localhost:6980/MathServiceInterface?wsdl") def result = proxy.add(1.0, 2.0) assert (result == 3.0) result = proxy.square(3.0) assert (result == 9.0)
Using WebTest
Using the WebTest variations makes sense if you are combining your tests into an acceptance test suite.
Here is how you would test it using traditional WebTest:
<steps> <invoke method="POST" contentFile="addreq.xml" soapAction="" url="http://localhost:6980/MathServiceInterface"/> <verifyXPath xpath="//addResponse/out[text()='3.0']"/> <invoke method="POST" contentFile="squarereq.xml" soapAction="" url="http://localhost:6980/MathServiceInterface"/> <verifyXPath xpath="//squareResponse/out[text()='9.0']"/> </steps>
Where addreq.xml would look something like:
<?xml version='1.0' encoding='UTF-8'?> <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <soap:Body> <add xmlns="http://DefaultNamespace"> <in0 xmlns="http://DefaultNamespace">1.0</in0> <in1>2.0</in1> </add> </soap:Body> </soap:Envelope>
and squarereq.xml would look something like:
<?xml version='1.0' encoding='UTF-8'?> <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <soap:Body> <square xmlns="http://DefaultNamespace"> <in0 xmlns="http://DefaultNamespace">3.0</in0> </square> </soap:Body> </soap:Envelope>
Alternatively, testing using groovy within WebTest would look like:
<steps>
<groovy>
import groovy.net.soap.SoapClient
def proxy = new SoapClient("http://localhost:6980/MathServiceInterface?wsdl")
def result = proxy.add(1.0, 2.0)
assert (result == 3.0)
result = proxy.square(3.0)
assert (result == 9.0)
</groovy>
</steps>
Note: you will need to place the jars mentioned on that page in your webtest lib directory (i.e. groovysoap, stax, jaf and mail jars) when using this variation.
The first approach (traditional webtest) produces more information in the test summary reporting but requires you to do more work (i.e. keep the requests around as XML). It depends if you already have those XML files around for other purposes, e.g. manual testing.
Using SOAPUI
soapui is a SOAP functional and load testing tool. It can use Groovy steps within its testcases. For further details, see the soapui documentation for the Groovy Step. This step supports data-driven tests, allows control of test execution and allows customised reporting.







