Test RESTful API Using SoapUI

This article explains how to test RESTful API using an open source tool SoapUI.

What is SoapUI?

SoapUI is an API testing tool for SOAP and REST APIs.

Why we need SoapUI
 
We can use SoapUI for testing of the web API and web services. SoapUI supports many things like functional testing, regression testing, load testing, and performance testing etc.
 
How to download and install SoapUI.
 
Download SoapUI from here and download 32-bit or 64-bit version which is suitable for your system. For installation, there is no need to do much. Please read the instructions and finish installation like the below screen.

ASP.NET 

ASP.NET 
 
ASP.NET 
 
ASP.NET 

How to test Web API using SoapUI. 

SoapUI supports many features but here I am going to explain how we can test the REST API.

To demonstrate API testing here, I have taken a small example of Web API application which has the below methods.

  • GetEmployees - This will return the list of employees
  • GetEmployee - It returns single employee based on the employeeId
  • PutEmployee - It updates existing employee based on the employeeId
  • PostEmployee - It creates a new employee in database
  • DeleteEmployee - It deletes existing employee based on the employeeId
Let's start with creating a new Web API application and SoapUI project set up for testing all methods.

Step 1 

Create a Web API application.

ASP.NET 

Step 2

Create a class and decorate with a few validations for properties because we are going to test it later using SoapUI.
  1. public class Employee  
  2.     {  
  3.         public int EmployeeId { getset; }  
  4.   
  5.         [Required(ErrorMessage="First Name is Required")]  
  6.         public string FirstName { getset; }  
  7.   
  8.         [Required(ErrorMessage = "Last Name is Required")]  
  9.         public string LastName { getset; }  
  10.   
  11.         public string City { getset; }  
  12.   
  13.         [MaxLength(6, ErrorMessage="Zipcode must be six digits")]  
  14.         public string ZipCode { getset; }   
  15.     }  

Step 3

Create a controller with all operation like GET, PUT, POST and DELETE and give the name as Employeecontroller.

  1. public class EmployeeController : ApiController  
  2.    {  
  3.        private EmployeeDBContext db = new EmployeeDBContext();  
  4.   
  5.        // GET api/Employee  
  6.        [HttpGet]  
  7.        public IEnumerable<Employee> GetEmployees()  
  8.        {  
  9.            return db.Employees.AsEnumerable();  
  10.        }  
  11.   
  12.        // GET api/Employee/5  
  13. fo
  14.        [HttpGet]  
  15.        public Employee GetEmployee(int id)  
  16.        {  
  17.            Employee employee = db.Employees.Find(id);  
  18.            if (employee == null)  
  19.            {  
  20.                throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.NotFound));  
  21.            }  
  22.   
  23.            return employee;  
  24.        }  
  25.   
  26.        // PUT api/Employee/5  
  27.        [HttpPut]  
  28.        public HttpResponseMessage PutEmployee(int id, Employee employee)  
  29.        {  
  30.            if (ModelState.IsValid && id == employee.EmployeeId)  
  31.            {  
  32.                db.Entry(employee).State = EntityState.Modified;  
  33.   
  34.                try  
  35.                {  
  36.                    db.SaveChanges();  
  37.                }  
  38.                catch (DbUpdateConcurrencyException)  
  39.                {  
  40.                    return Request.CreateResponse(HttpStatusCode.NotFound);  
  41.                }  
  42.   
  43.                return Request.CreateResponse(HttpStatusCode.OK);  
  44.            }  
  45.            else  
  46.            {  
  47.                return Request.CreateResponse(HttpStatusCode.BadRequest);  
  48.            }  
  49.        }  
  50.   
  51.        // POST api/Employee  
  52.        [HttpPost]  
  53.        public HttpResponseMessage PostEmployee(Employee employee)  
  54.        {  
  55.            if (ModelState.IsValid)  
  56.            {  
  57.                db.Employees.Add(employee);  
  58.                db.SaveChanges();  
  59.   
  60.                HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.Created, employee);  
  61.                response.Headers.Location = new Uri(Url.Link("DefaultApi"new { id = employee.EmployeeId }));  
  62.                return response;  
  63.            }  
  64.            else  
  65.            {  
  66.                return Request.CreateResponse(HttpStatusCode.BadRequest);  
  67.            }  
  68.        }  
  69.   
  70.        // DELETE api/Employee/5  
  71.        public HttpResponseMessage DeleteEmployee(int id)  
  72.        {  
  73.            Employee employee = db.Employees.Find(id);  
  74.            if (employee == null)  
  75.            {  
  76.                return Request.CreateResponse(HttpStatusCode.NotFound);  
  77.            }  
  78.   
  79.            db.Employees.Remove(employee);  
  80.   
  81.            try  
  82.            {  
  83.                db.SaveChanges();  
  84.            }  
  85.            catch (DbUpdateConcurrencyException)  
  86.            {  
  87.                return Request.CreateResponse(HttpStatusCode.NotFound);  
  88.            }  
  89.   
  90.            return Request.CreateResponse(HttpStatusCode.OK, employee);  
  91.        }  
  92.   
  93.        protected override void Dispose(bool disposing)  
  94.        {  
  95.            db.Dispose();  
  96.            base.Dispose(disposing);  
  97.        }  
  98.    }  

Step 4

Create a filter to validate model properties during post operation. 

  1. public class ValidateModelAttribute : ActionFilterAttribute  
  2.     {  
  3.         public override void OnActionExecuting(HttpActionContext actionContext)  
  4.         {  
  5.             if (!actionContext.ModelState.IsValid)  
  6.             {  
  7.                 actionContext.Response = actionContext.Request.CreateErrorResponse(  
  8.                     HttpStatusCode.BadRequest, actionContext.ModelState);  
  9.             }  
  10.         }  
  11.     }  

Step 5

Register Filter in WebApiconfig.cs file.

  1. public static void Register(HttpConfiguration config)  
  2.         {  
  3.             config.Filters.Add(new ValidateModelAttribute());  
  4.   
  5.             config.Routes.MapHttpRoute(  
  6.                 name: "DefaultApi",  
  7.                 routeTemplate: "api/{controller}/{id}",  
  8.                 defaults: new { id = RouteParameter.Optional }  
  9.             );  
  10.         }  

Step 6

Build and run our Web API application. We are ready with our API application. 

Now, let's set up our API application in SoapUI for testing all operations. Please follow the steps for the same setup and testing.

Step 1

Open SoapUI application from Start menu or shortcut icon. 

ASP.NET 

Step 2

Once SoapUI tool is open, then go to REST icon and once click, you will get the second screen where you can set API URL and click on OK.
 
ASP.NET 
 
ASP.NET 
 
Step 3

Create a new Employee using post method. It will create a new employee in the database as well.

ASP.NET

Step 4

Let's test GetEmployee method which is HttpGet method and it's taking id as a parameter.

 ASP.NET 

Step 5

Let's test update operation for EmployeeId 1 which we just now created. 

 ASP.NET

Step 6

Test GetEmployees which will return all employees from the database. 

ASP.NET 
 
Step 7

Finally, test DeleteEmployee operation from SoapUI. We will delete employeeId 2.

ASP.NET 
 
Step 8

Finally, I will test my validation for required fields, first name and last name. To create this scenario, I will try to pass firstName and LastName empty in my post operation as below it will throw message "The request is invalid" and it will throw our validation message which we had written in our model for first name and last name.

ASP.NET 

Conclusion

Here we had tested all operations using SoapUI. It's very easy to use SoapUI for testing Web APIs or web services and it has more features like functional testing, load testing, performance testing and regression testing, etc. It is an open source tool so we can download the free version and test our RESTful service very easily. 

Thanks for reading...

This helped me a lot in real-time. Hope it will help you as well.


Similar Articles