Web Service Testing: A Beginner's Guide

 

What is WebService? Why is it Needed?

            

In General, software applications are developed to be consumed by the human beings, where a person sends a request to a software service which in-turn returns a response in human readable format.

In the modern era of technology if you want to build a software application you don't need to build each and everything from scratch. There are lots of readymade services available which you can plug into your application and you can start providing those services in your application.

For example you want to display whether forecast information you don't need to collect, process and render the data in your application. You can buy the services from the people who already well-established in processing and publishing such kind of data.

Web services allow us to do these kind of implementations. Web Services is the mechanism or the medium of communication through which two applications / machines will exchange the data irrespective of their underline architecture and the technology.  

As an example, consider the following WebService

http://www.webservicex.net/stockquote.asmx?op=GetQuote

It gives Share Value for a Company.

Let's find share price for Google (Symbol: GOOG )

Web Service Testing: A Beginner's Guide

The response XML gives the stock price.

Web Service Testing: A Beginner's Guide

This WebService can be called by a Software Application using SOAP or HTTP protocol.  

Web Services can be implemented in different ways, but the following two are the popular implementations approaches.

  1. SOAP (Simple Object Access Protocol)
  2. REST (Representational State Transfer architecture)
 

SOAP

SOAP is a standard protocol defined by the W3C Standard for sending and receiving web service requests and responses.

SOAP uses the XML format to send and receive the request and hence the data is platform independent data. SOAP messages are exchanged between the provider applications and receiving application within the SOAP envelops.

As SOAP uses the simple http transport protocol, its messages are not got blocked by the firewalls.  

REST

 

REST means REpresentational State Transfer; it is an architecture that generally runs over HTTP. The REST style emphasizes the interactions between clients and services, which are enhanced by having a limited number of operations. REST is an alternative to SOAP (Simple Object Access Protocol) and instead of using XML for request REST uses simple URL in some cases. Unlike SOAP, RESTFUL applications uses HTTP build in headers to carry meta-information.

There are various code that REST use to determine whether user has access to API or not like code 200 or 201 indicates successful interaction with response body while 400 indicates a bad request or the request URI does not match the APIs in the system. All API request parameters and method parameters can be sent via either POST or GET variables.  

Rest API supports both XML and JSON format. It is usually preferred for mobile and web apps as it makes app work faster and smoother There is one more thing one needs to learn  

WSDL

WSDL (Web Services Description Language) is an XML based language which will be used to describe the services offered by a web service.

WSDL describes all the operations offered by the particular web service in the XML format. It also defines how the services can be called, i.e what input value we have to provide and what will be the format of the response it is going to generate for each kind of service.

 

Web Service Testing

 

Web Services Testing basically involves

  1. Understand the WSDL file
  2. Determine the operations that particular web service provides
  3. Determine the XML request format which we need to send
  4. Determine the response XML format
  5. Using a tool or writing code to send request and validate the response

Suppose we want to test a WebService which provides Currency Conversion Facility. It will the current conversion rates between the different countries currency. This service we can use in our applications to convert the values from one currency to the other currency.

Now lets look at above steps

Step 1 to 4: Understading WSDL and determining operations & XML formats

 

Currency Convertor WSDL file can be seen @ (http://www.webservicex.net/CurrencyConvertor.asmx?wsdl) which will give the information about the Currency Convertor web service methods which it will support, the parameter which we need pass and the type of parameters… etc

Web Service Testing: A Beginner's Guide

Web Service Testing: A Beginner's Guide  

Step 5: Using a tool or writing code to send request and validate the response

 

There are lots of tools available to test web services. SoapUI is one of the popular tool which will help us to test the web services. In fact you can use the any programing language which is capable of sending the XML request to the web service provider application over the http and able to parse and validate the response XML against the expected result. In our case, we will test the WebService

  1. Using Java
  2. Using SoapUI

PART 1) WebService Testing Using Apache Axis2 API (Java).

Generally web service takes the request and sends the response in the XML format.

Apache Axis2 API project is a Java implementation API, which will be used to create the web services for both server side (service provider) and client side (service consumer).

Axis2 is capable of sending SOAP messages and Receives & Processes the SOAP messages. We can write a small Java program using the API to create the web service. Axis2 will generate the WSDL from Java program which will be used to communicate the services offered by the web service. We can use the same Axis2 to generate the Java class (stub) from WSDL file which we can use as a client program to generate the web service request, to send the request to the service end point and to process the response.

  1. Basically we will create a simple Java program in which we will instantiate the stub class.
  2. Using the stub we will invoke the request method by passing all the required information.
  3. Stub program will convert that request into XML request format and sends it the service end point which will read the request and processes the request and sends the response in XML format.
  4. The XML response will be converted into Java class by stub and returned to the actual program.

Let's look at above steps in detail

Step a) Download the axis2 API @ https://axis.apache.org/axis2/Java/core/download.cgi & Set the environment variable 'AXIS2_HOME'

Web Service Testing: A Beginner's Guide  

Step b) Create a folder to keep all the generated artifacts

Ex : C:\Axis\Projects\CurrencyConverter

Step c) Open the command prompt and navigate to the folder structure where you want to generate the artifacts and Run the following command which will generate the stubs

%AXIS2_HOME%\bin\WSDL2Java -uri http://www.webservicex.net/CurrencyConvertor.asmx?wsdl -p org.apache.axis2.currencyconvertor -d adb –s

Web Service Testing: A Beginner's Guide

Step d) Once the command is successfully run, you will see the folder with required files.

Web Service Testing: A Beginner's Guide  

Step e) Next we have to create the client program, through which we will send the actual request using the generated stubs. Open the eclipse and create the new Java project and select the folder which we have created above.  

Web Service Testing: A Beginner's Guide

Step f) Add all the axis2 related jars to project build path, which will be there in lib folder of the axis2 software folder

(for ex : C:\Axis\axis2-1.6.2\lib)

Web Service Testing: A Beginner's Guide

Step g) Create a new Java class (ex : Client.Java) and instantiate stub object. Using the stub object we can call all the supported methods of the particular WebService.  

Web Service Testing: A Beginner's Guide

Client.Java Program

 package org.apache.axis2.currencyconvertor;
		
 import org.apache.axis2.currencyconvertor.CurrencyConvertorStub.ConversionRate;
		import org.apache.axis2.currencyconvertor.CurrencyConvertorStub.ConversionRateResponse;
		import org.apache.axis2.currencyconvertor.CurrencyConvertorStub.Currency;
		
 public
				class Client{
				
			public
					static
							void main(Java.lang.String args[]){
							
			try{
			          
            //Create the stub object by passing the service end point url
		           CurrencyConvertorStub stub = new CurrencyConvertorStub( 	"http://www.webservicex.net/CurrencyConvertor.asmx");
					
             //ConversionRate is the class which we have to use mention the from and to currency
		            //ConversionRate object will be the parameter for the conversionRate operation
		           ConversionRate conversionRate =  new ConversionRate();
			            conversionRate.setFromCurrency(Currency.USD);
			            conversionRate.setToCurrency(Currency.INR);
			        
             //Create the ConversionRateResponse object, which is going to be used to catch the response
		            //call the conversionRate service using the stub object
		            ConversionRateResponse conversionRateResponse  = stub.conversionRate(conversionRate);
		         
            //We can use the conversionRateResponse object to retrieve the response of the ConversionRate Service
		           System.out.println("Conversion Rate from INR to USD : " + conversionRateResponse.getConversionRateResult());
				
       } catch(Exception e){
			          e.printStackTrace();
		        }
		    }
		 
 }
		
 

PART 2) Using SoapUI to Test the WebService

 

In SoapUI  

  1. Go to File > New Soap Project
  2. Enter the project Name and the WSDL URI location
  3. Click OK
 

Web Service Testing: A Beginner's Guide

 
  1. Expand the first request and double click on the 'Request1'. It will display the SOAP request in the XML format.
  2. Enter the From Currency and To Currency
  3. Click on the submit button
  4. Response XML will be displayed right side pane.
 

Web Service Testing: A Beginner's Guide  

As you may conclude, usage of tools like SoapUI expedites your WebService Testing Effort. Hence SoapUi will be focus of our learning in the succeeding tutorials.

Summary

  • Software Applications communicate and exchange data with each other using a WebService
  • SOAP and REST are 2 popular protocols to create a WebService
  • SOAP supports XML based data exchange
  • REST support XML, Json or exchange of data in simple URL.
  • WSDL is XML based language which will be used to describe the services offered by a web service. SOAP is defined using WSDL.
  • To test WebService you can
    • Create your own code. For instance use Axis2 API for Java
    • Use WebService Test Automation tools like SoapUI
  • Automation Tools like SoapUI will jumpstart your testing efforts, will require less coding effort compared to creating your own code using Axis2 API
 

FAQ

What is Difference between WebService and WebAPI?

Web Service

Web API

  • Defined by W3C, all communication & data exchange is based on XML
  • Web API communication & data exchange could be XML, JSON or plain data
  • It has defined standards – WSDL
  • No defined standard
  • You cannot compress the data but you can compress the HTML request
  • You can compress the data
  • Example: SOAP
  • Example: REST

Learn more about Web API Testing

This tutorial is made possible with contributions of Mr. Narender Reddy Nukala