Introduction
An introduction to the SOAP UI was covered in the Part 1 of
this article which can be seen here. As part of that, we have seen what is a
SOAP UI, how to create a sample project, how to run a request and the types
of assertions available in SOAP UI. In this part, we will add assertions
to our web service, the simple interest calculator.
Background
As mentioned in Part 1, assertions are used to measure the
performance of a service. In the following section, I have added various
assertions that I have used across my projects.
Prerequisites
A small change has been made to the service. The response of the service has been
wrapped to a property class; the updated code is given below.
public class SimpleInterestResponse
{
private int _returnCode;
private string _statusMessage;
private double _simpleInterest;
public SimpleInterestResponse()
{
}
public int ReturnCode
{
get
{
return _returnCode;
}
set
{
_returnCode = value;
}
}
public string StatusMessage
{
get
{
return _statusMessage;
}
set
{
_statusMessage = value;
}
}
public double SimpleInterest
{
get
{
return _simpleInterest;
}
set
{
_simpleInterest = value;
}
}
}
Here is the web service method:
[WebMethod]
public SimpleInterestResponse SimpleInterestcal(int principalAmount, float rateofInterest, int loanPeriod)
{
SimpleInterestResponse objSimIntr = new SimpleInterestResponse();
try
{
if (principalAmount == 0)
{
throw new Exception("Principal cant be zero (0).");
}
else if (principalAmount == 5000)
{
throw new TimeoutException("Web service timed out");
}
else if (principalAmount == 5001)
{
throw new FormatException("Invalid Principal amount");
}
else if (principalAmount == 5002)
{
throw new Exception("A general exception has occured");
}
else if (principalAmount == 5003)
{
System.Threading.Thread.Sleep(10000);
}
else
{
objSimIntr.ReturnCode = 0;
objSimIntr.StatusMessage = "Success";
objSimIntr.SimpleInterest = (principalAmount * rateofInterest * loanPeriod) / 100;
}
return objSimIntr;
}
catch (TimeoutException tExp)
{
objSimIntr.ReturnCode = 50;
objSimIntr.StatusMessage = "Timeout : " + tExp.Message; ;
objSimIntr.SimpleInterest = 0;
return objSimIntr;
}
catch (FormatException fExp)
{
objSimIntr.ReturnCode = 60;
objSimIntr.StatusMessage = "Invalid Format :" + fExp.Message;
objSimIntr.SimpleInterest = 0;
return objSimIntr;
}
catch (Exception exp)
{
objSimIntr.ReturnCode = 70;
objSimIntr.StatusMessage = "General Exception :" + exp.Message;
objSimIntr.SimpleInterest = 0;
return objSimIntr;
}
finally
{
}
}
The service will return three parameters:
- A return code, 0 for success and a non-zero integer for error conditions
- Status message – a brief description of error condition
- Simple interest – 0 generally means an error condition / validation failure
The service is capable of returning three exceptions, timeout, format and general exception and in addition, if the principal amount is 5003, the response will be delayed by 10 seconds.
- Add a test suite to the SOAP UI Project and add a new test case.
- Drag and drop the Request 1 under the test case. The request will be added under test step 1 as shown in the figure below;
- Double click on the request 1 to open the request window on the right pan.
- Click on the assertion button (rounded). This will open the assertion selection drop down.
- Select the invalid HTTP Status code and click OK.
- This will open a small window where we can specify the code that we want to check.
- Give 401 (unauthorized), 404 (Not found) etc. and as many as you required and click OK.
- In the same way, add an assertion for valid http code (200).
- Add a response SLA and specify the value as 5000 as shown in figure below. This means any response obtained after 5 seconds are considered as invalid.
- Run the test. All will be green meaning all tests are passed.
- Now change the principal amount as 5003 and run the test. The service will delay the response by 10 seconds there by triggering a SLA violation in the SOAP UI.
- I have added a Xpath match which will check whether the return code is 0 and raise alert if it is not. For example, if the principal amount is selected as 5001, then return code will be 60 and an assertion failure will occur. Adding a xpath match is shown below.
- Similarly the “contains” and “schema compliance” will also work in the same way.
- Depending on the requirements, one or more assertions can be added to a SOAP UI project.
- I have stopped the web service and so all the assertions have failed!
These are some of the assertions that can be added to test the quality of the web service. If the assertions provided in the tool does not match your requirements,
using Groovy, we can write own assertions. More assertions can be added to a load test as well which I will cover in the next part.
Points of Interest
In this part we learnt how to add various assertions to a web service.
History
Initial version.