Click here to Skip to main content
Click here to Skip to main content

SOAP UI - Part 2 - Assertions

, 29 Jun 2012 CPOL
Rate this:
Please Sign up or sign in to vote.
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.

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:

[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;

  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.

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

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

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



  13. Similarly the “contains” and “schema compliance” will also work in the same way.
  14. Depending on the requirements, one or more assertions can be added to a SOAP UI project.
  15. 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.

License

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

Share

About the Author

Jobless Creature
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 --
| Advertise | Privacy | Mobile
Web04 | 2.8.141015.1 | Last Updated 29 Jun 2012
Article Copyright 2012 by Jobless Creature
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid