Click here to Skip to main content
15,868,098 members
Articles / DevOps / Testing

SOAP UI - Part 2 - Assertions

Rate me:
Please Sign up or sign in to vote.
0.00/5 (No votes)
29 Jun 2012CPOL3 min read 23.2K   10  
This is a continuation of my article on SOAP UI Introduction

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.

C#
public class SimpleInterestResponse
{
    private int _returnCode;
    private string _statusMessage;
    private double _simpleInterest;
    public SimpleInterestResponse()
    {
        //
    // TODO: Add constructor logic here
    //
    }
 
    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:

C#
[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:

  1. A return code, 0 for success and a non-zero integer for error conditions
  2. Status message – a brief description of error condition
  3. 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.

  1. Add a test suite to the SOAP UI Project and add a new test case.
  2. 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;

    Image 1

  3. Double click on the request 1 to open the request window on the right pan.
  4. Click on the assertion button (rounded). This will open the assertion selection drop down.

    Image 2

  5. Select the invalid HTTP Status code and click OK.
  6. This will open a small window where we can specify the code that we want to check.
  7. Give 401 (unauthorized), 404 (Not found) etc. and as many as you required and click OK.
  8. Image 3

    Image 4

  9. In the same way, add an assertion for valid http code (200).
  10. 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.

    Image 5

  11. Run the test. All will be green meaning all tests are passed.
  12. 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.

    Image 6

  13. 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.

    Image 7

    Image 8

  14. Similarly the “contains” and “schema compliance” will also work in the same way.
  15. Depending on the requirements, one or more assertions can be added to a SOAP UI project.
  16. I have stopped the web service and so all the assertions have failed!
  17. Image 9

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.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior) TATA Communications
India India
I have a total experience of around 5 years out of which 4 years in MS technologies. I have a passion towards coding.

Comments and Discussions

 
-- There are no messages in this forum --