Soap WebService testing using Stub generation

In this tutorial I will show an easy way on how to generate Stubs needed to call SOAP webservice methods from Java (or in this tutorial, with Groovy).

The SOAP webservice that I have in mind for this demo is the free Global Weather WebService and we will be needing its WSDL:

1
http://www.webservicex.net/globalweather.asmx?WSDL

Will now go ahead and create a new Maven project in IntelliJ IDEA 15 and then Add Framework Support and select check the Webservice client.
You can also find a link to the GitHub project at the end of this post.

Before generating the stubs, we need a few dependencies to be added in the pom.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
<dependencies>
        <dependency>
            <groupId>org.apache.axis</groupId>
            <artifactId>axis</artifactId>
            <version>1.4</version>
        </dependency>

        <dependency>
            <groupId>axis</groupId>
            <artifactId>axis-wsdl4j</artifactId>
            <version>1.5.1</version>
        </dependency>

        <dependency>
            <groupId>axis</groupId>
            <artifactId>axis-jaxrpc</artifactId>
            <version>1.4</version>
        </dependency>

        <dependency>
            <groupId>commons-discovery</groupId>
            <artifactId>commons-discovery</artifactId>
            <version>0.5</version>
        </dependency>

        <dependency>
            <groupId>org.codehaus.groovy</groupId>
            <artifactId>groovy-all</artifactId>
            <version>2.4.5</version>
        </dependency>
    </dependencies>

What we need to do now is right click on a folder in our project and select the last option WebServices -> Generate Java Code from Wsdl

IDEA

After the Stub generation is finished, we will find the following files inside the stubs package

IDEA Project Structure

We are aiming to be able to call the methods of the global webservice:

1
2
public String getWeather(String cityName, String countryName)
public String getCitiesByCountry(String countryName)

but in order to do that, we need to get ourselves an instance of the GlobalWeatherLocator and furthermore, access the GlobalWeatherSoap_PortType object which has the 2 methods that we’re interested in.
A quick note here: Don’t be fooled by the “String” return type of these 2 methods, the String is actually … XML so we’re going to need a XmlSlurper to parse it and perform asserts.

Using Groovy, this code will look something like this:

1
2
3
4
5
6
7
8
9
10
11
12
import stubs.GlobalWeather
import stubs.GlobalWeatherLocator
import stubs.GlobalWeatherSoap_PortType


GlobalWeather globalWeather = new GlobalWeatherLocator()
GlobalWeatherSoap_PortType globalWeatherSoap = globalWeather.getGlobalWeatherSoap()

def xmlResponse = globalWeatherSoap.getWeather("Bucuresti", "Romania")

def parsedXML = new XmlSlurper().parseText(xmlResponse)
assert parsedXML.Status == "Success"

The code for the project can be found on GitHub – WebServiceClientStubs

Hope you’ve enjoyed generating some stubs.

Leave a Reply

Your email address will not be published. Required fields are marked *