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.
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.
Step 2
Create a class and decorate with a few validations for properties because we are going to test it later using SoapUI.
- public class Employee
- {
- public int EmployeeId { get; set; }
-
- [Required(ErrorMessage="First Name is Required")]
- public string FirstName { get; set; }
-
- [Required(ErrorMessage = "Last Name is Required")]
- public string LastName { get; set; }
-
- public string City { get; set; }
-
- [MaxLength(6, ErrorMessage="Zipcode must be six digits")]
- public string ZipCode { get; set; }
- }
Step 3
Create a controller with all operation like GET, PUT, POST and DELETE and give the name as Employeecontroller.
- public class EmployeeController : ApiController
- {
- private EmployeeDBContext db = new EmployeeDBContext();
-
-
- [HttpGet]
- public IEnumerable<Employee> GetEmployees()
- {
- return db.Employees.AsEnumerable();
- }
-
-
fo- [HttpGet]
- public Employee GetEmployee(int id)
- {
- Employee employee = db.Employees.Find(id);
- if (employee == null)
- {
- throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.NotFound));
- }
-
- return employee;
- }
-
-
- [HttpPut]
- public HttpResponseMessage PutEmployee(int id, Employee employee)
- {
- if (ModelState.IsValid && id == employee.EmployeeId)
- {
- db.Entry(employee).State = EntityState.Modified;
-
- try
- {
- db.SaveChanges();
- }
- catch (DbUpdateConcurrencyException)
- {
- return Request.CreateResponse(HttpStatusCode.NotFound);
- }
-
- return Request.CreateResponse(HttpStatusCode.OK);
- }
- else
- {
- return Request.CreateResponse(HttpStatusCode.BadRequest);
- }
- }
-
-
- [HttpPost]
- public HttpResponseMessage PostEmployee(Employee employee)
- {
- if (ModelState.IsValid)
- {
- db.Employees.Add(employee);
- db.SaveChanges();
-
- HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.Created, employee);
- response.Headers.Location = new Uri(Url.Link("DefaultApi", new { id = employee.EmployeeId }));
- return response;
- }
- else
- {
- return Request.CreateResponse(HttpStatusCode.BadRequest);
- }
- }
-
-
- public HttpResponseMessage DeleteEmployee(int id)
- {
- Employee employee = db.Employees.Find(id);
- if (employee == null)
- {
- return Request.CreateResponse(HttpStatusCode.NotFound);
- }
-
- db.Employees.Remove(employee);
-
- try
- {
- db.SaveChanges();
- }
- catch (DbUpdateConcurrencyException)
- {
- return Request.CreateResponse(HttpStatusCode.NotFound);
- }
-
- return Request.CreateResponse(HttpStatusCode.OK, employee);
- }
-
- protected override void Dispose(bool disposing)
- {
- db.Dispose();
- base.Dispose(disposing);
- }
- }
Step 4
Create a filter to validate model properties during post operation.
- public class ValidateModelAttribute : ActionFilterAttribute
- {
- public override void OnActionExecuting(HttpActionContext actionContext)
- {
- if (!actionContext.ModelState.IsValid)
- {
- actionContext.Response = actionContext.Request.CreateErrorResponse(
- HttpStatusCode.BadRequest, actionContext.ModelState);
- }
- }
- }
Step 5
Register Filter in WebApiconfig.cs file.
- public static void Register(HttpConfiguration config)
- {
- config.Filters.Add(new ValidateModelAttribute());
-
- config.Routes.MapHttpRoute(
- name: "DefaultApi",
- routeTemplate: "api/{controller}/{id}",
- defaults: new { id = RouteParameter.Optional }
- );
- }
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.
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.
Step 3
Create a new Employee using post method. It will create a new employee in the database as well.
Step 4
Let's test GetEmployee method which is HttpGet method and it's taking id as a parameter.
Step 5
Let's test update operation for EmployeeId 1 which we just now created.
Step 6
Test GetEmployees which will return all employees from the database.
Step 7
Finally, test DeleteEmployee operation from SoapUI. We will delete employeeId 2.
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.
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.