Library

6 Different Ways to Testing a Web Service

Archived Content
This article is provided for historical perspective only, and may not reflect current conditions. Please refer to relevant product page for more up-to-date product information and resources.
  • By Charitha Kankanamge
  • 21 Jul, 2008

Introduction

There are multiple approaches to invoking Web services, regardless of the platform or technology used to built such services. If a service consumer has access to the contract (WSDL) file of a given Web service, then, that particular service can be invoked via either a tool or a client application, whichever is the preferred option. This tutorial focuses on six different approaches to testing and invoking Web services.

Applies To



Apache Axis2 version 2.3 or higher
EviWare SOAPUI version 2.0 or higher
Apache JMeter version 2.2 or higher
WSO2 WSAS 2.1 or higher

Table of Contents

In this tutorial, we invoke a public Web service hosted at www.webservicex.net. Out of the many different categories of Web services hosted there, we use the Periodic Table Web service categorized under standards and Lookup data. Go to http://www.webservicex.net/WCF/ServiceDetails.aspx?SID=37 and you will see the Periodic Table sample Web service there. This Web service exposes four operations, GetAtoms, GetAtomicWeight, GetAtomicNumber and GetElementSymbol. For the purpose of the demonstration, we will use the GetAtoms operation. You will see the following sample SOAP request message on that page:

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
    <soap:Body>
        <GetAtoms xmlns="http://www.webserviceX.NET" />
    </soap:Body>
</soap:Envelope>

As the service consumer, we have to construct the above SOAP request using any of the available tools. You can choose any from the approaches given below. Follow the tutorial to the end and you will likely have made a decision on which approach to use by then. Let's look at each of the approaches in detail now.

1. Axis2 Generated Client

Apache Axis2 provides a useful code generation tool referred to as WSDL2Java, that can be used to generate client stubs (proxy) in order to interact with Web services. This Axis2 code generator is able to handle both client and server-side code generation. It supports different data binding frameworks and thus supports generating code through any one of them. In this example, we use the Axis2 default data binding mechanism referred to as the ADB. Go to Axis2_HOME/bin and run WSDL2java.sh with the following options. Note that AXIs2_HOME is the location to which you extracted Axis2 binary distribution.

wsdl2java -uri http://www.webservicex.net/periodictable.asmx?wsdl -o out

Client stubs will be generated and saved in Axis2_home/bin/out directory. You will see a build.xml file and a src directory as a part of such generated artifacts.

Next, open your favourite IDE and create a project from the build.xml file generated. (In eclipse, follow New -->Java Project from existing Ant Build) All required Axis2 libraries will be added to your IDE class path. Now, start creating a new class by adding the following lines of code:

public class Client {
    public static void main(String[] args)throws AxisFault {
        PeriodictableStub stub = new PeriodictableStub();
        PeriodictableStub.GetAtoms atoms = new PeriodictableStub.GetAtoms();

        try {
            PeriodictableStub.GetAtomsResponse response = stub.GetAtoms(atoms);
            System.out.println(response.getGetAtomsResult());
        } catch (RemoteException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

Compile and run the client. You will see all elements in the periodic table as output.

2.Axis2 ServiceClient API

In this approach, we will construct the payload of a request SOAP message using Axis2 serviceClient. Axis2 client API consists of two sub APIs known as ServiceClient and OperationClient. The ServiceClient API is targeted for beginners and it can be used to invoke any Web service in more convenient manner, as compared to the previous approach. In the following example, we use the Axis2 ServiceClient API to invoke our Web service. Start creating the class with the following lines of code:

public class AxiomClient {
    public static OMElement createPayLoad(){
        OMFactory fac = OMAbstractFactory.getOMFactory();
        OMNamespace omNs = fac.createOMNamespace("http://www.webserviceX.NET", "ns1");
        OMElement getAtoms = fac.createOMElement("getAtoms", omNs);
        return getAtoms;
    }

    public static void main(String[] args)throws AxisFault {
        OMElement payload = createPayLoad();
        ServiceClient serviceclient = new ServiceClient(); 
        Options opts = new Options();
        opts.setTo(new EndpointReference("http://www.webservicex.net/periodictable.asmx"));
        opts.setAction("http://www.webserviceX.NET/GetAtoms");
        serviceclient.setOptions(opts);
        OMElement result =serviceclient.sendReceive(payload);
        System.out.println(result);
    }
}

Compile and run the client. Make sure to add all Axis2 library jars into class path. You will get the same result as in the first approach.

3.REST/POX based invocation

Using Apache Axis2, you can access Web services that have simple type parameters via HTTP GET.

Open your favourite browser and enter the following URL.

http://www.webservicex.net/periodictable.asmx/GetAtoms

The result will be same as the previous steps and all the elements in periodic table will be displayed in your browser.

4.SOAPUI

SoapUI is Free and Open Source tool used for Web services testing. Let's see how our periodic table sample Web service can be invoked using SOAPUI.

Start SOAPUI. SOAPUI GUI console will be launched. Select File-->New WSDL Project New WSDL Project dialog box will be displayed. Give a name for the project and enter the path for the periodic table wsdl file (i.e. http://www.webservicex.net/periodictable.asmx?wsdl) Make sure to select the check box next to "Create requests" Click OK to continue. SoapUI will generate a sample request for all operations available in the WSDL as illustrated below:

Select "GetAtoms" operation and click on the play button. The results will be displayed in the right pane.

5.Apache JMeter

Apache JMeter is a java desktop application designed to load and test performance and functionality. It has support for web services testing using SOAP/XML-RPC Request and Web Service samplers.

Here we are going to use SOAP/XML-RPC Sampler to invoke periodic table web service. Go to JMETER_HOME/bin and run jmeter.sh{bat} Note that JMeter_HOME is the location where you extracted Jmeter binary distribution. The swing based JMeter console will be launched. Right click on Test plan element and add a new thread group

Right click on Thread Group and select Add-->Sampler-->SOAP/XML-RPC Sampler

Copy and paste the above sample SOAP request message in SOAP/XML-RPC data section

Enter "http://www.webservicex.net/periodictable.asmx" as the URL

GiveĀ  "http://www.webserviceX.NET/GetAtoms" as the soapAction Add a listener to visualize the results. (Right click on thread group element, select Add-->Listener-->View Results in Tree)



Save the JMeter test plan and run the test. The results will be shown in "Response Data" section of result tree.

6.WSO2 WSAS TryIt

WSO2 WSAS Tryit is a GUI tool used to try out any web service. You can invoke remote web service just providing the url of the WSDL. Go to WSO2WSAS_HOME/bin and start wso2wsas by running wso2wsas.sh Access WSO2 WSAS management console using http://localhost:9762 Home page of the WSAS management console will be displayed. Select "TryIt" in the left menu.

Tryit

"Tryit" page will be landed. Enter "http://www.webservicex.net/periodictable.asmx?wsdl" as wsdl1.1 location and click on Tryit. You will get a page as given below.

Click "GetAtoms". The elements of the periodic table will be displayed as the result. WSO2 OxygenTank hosts a public instance of Tryit tool which can be used to test your web service without downloading any client application in to your machine. You can access the online Tryit utility through http://tools.wso2.org/tools_tryit/ and make use of it.

Conclusion

In this tutorial, we have looked at six different approaches to invoking and testing a simple Web service. Most of the tools used, simplifies the service invocation process by generating the SOAP envelope or the XML request message according to a given WSDL. You can decide the most comfortable approach based on your requirements. Though the sample was based on a service that exchanged simple datatypes, you could also try out above approaches with complex data types and experience for yourself and decide as to which method is the best. There are pros and cons to using each different tool and it is the responsibility of a service consumer to select the most appropriate tool for the job at hand.

Author

Charitha is Manager of SQA. charitha at wso2 dot com

About Author

  • Charitha Kankanamge
  • Senior Manager, Support
  • WSO2 Inc