In the previous articles in this Clean Architecture series, we implemented the different layers of an ASP.NET Core Web API application, including the Domain, Application, Infrastructure, and Presentation layers. We also configured Entity Framework Core and applied database migrations to create the required database tables.
Now that our Employee Management API is ready, it's time to test the implemented endpoints.
In this article, we will explore how to use Swagger UI to test our ASP.NET Core Web API endpoints. We will perform CRUD operations, including creating, retrieving, updating, and deleting employee records, and verify the results directly through Swagger.
By the end of this article, you will understand how to test your Clean Architecture-based Web API using Swagger UI.
ASP.NET Clean Architecture: Test the API Using Swagger
What is Swagger?
Swagger is a toolset that helps developers document, visualize, and interact with RESTful APIs. It provides an interactive user interface called Swagger UI, where developers can explore available API endpoints and execute HTTP requests without requiring a separate API testing tool.
Key benefits of Swagger:- Provides interactive API documentation.
- Displays available HTTP endpoints and their request methods.
- Allows developers to test API endpoints directly from the browser.
- Displays API responses and HTTP status codes.
- Helps developers understand request parameters and response models.
Configure Swagger in ASP.NET Core Web API
For a .NET 8 or .NET 10 application using Swashbuckle, install the following NuGet package if it is not already installed.
</> powershell
Install-Package Swashbuckle.AspNetCore
Then open
Program.cs and configure Swagger services.
</> C#
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
Add the following middleware before
app.Run():
</> C#
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}
The Swagger UI will be available at:
</> Plain Text
https://localhost:<port>/swagger
Note: If you already configured Swagger in your project, you do not need to add the configuration again.
Run the ASP.NET Core Web API
- Open the
CleanArchitectureDemosolution in Visual Studio 2022. - Set the API project as the startup project.
- Ensure the SQL Server connection string is configured correctly.
- Make sure database migrations have been applied.
- Press
F5or click the Run button.
Once the application starts, open Swagger UI in your browser.
Test Employee API Endpoints Using Swagger
Your Employee Management API contains the following CRUD endpoints.| HTTP Method | Endpoint | Description |
|---|---|---|
| GET | /api/employees | Get all employees |
| GET | /api/employees/{id} | Get employee by ID |
| POST | /api/employees | Create an employee |
| PUT | /api/employees/{id} | Update an employee |
| DELETE | /api/employees/{id} | Delete an employee |
5.1 Test GET – Retrieve All Employees
- Expand the
GET /api/employeesendpoint. - Click the Try it out button.
- Click Execute to send the API request.
Expected response: The response from the API will be like this.
</> JSON
[
{
"id": 1,
"name": "Roy",
"email": "roy@example.com",
"salary": 50000
},
{
"id": 2,
"name": "Ruchit",
"email": "ruchit@example.com",
"salary": 60000
}
]
Similarly test all the API endpoints
Verify API Responses and HTTP Status Codes
| Status Code | Description |
|---|---|
| 200 OK | Request completed successfully. |
| 201 Created | A new employee was created. |
| 204 No Content | Request succeeded without returning a response body. |
| 400 Bad Request | Invalid request data. |
| 404 Not Found | Employee record does not exist. |
| 500 Internal Server Error | Unexpected server-side error. |
Verify Employee Records in SQL Server
After performing CRUD operations through Swagger, open SQL Server Management Studio (SSMS).
Execute:</> powershell
SELECT * FROM dbo.Employees;
Verify that:
- Newly created employees are available in the table.
- Updated employee details are reflected in the table.
- Deleted employees are no longer available in the table.
This confirms that the API endpoints are communicating with the database through the Infrastructure layer.
Common Issues and Troubleshooting
| Issue | Possible Solution |
|---|---|
| Swagger page not found | Verify Swagger middleware configuration. |
| Unable to connect to database | Check the SQL Server connection string. |
| 404 Not Found | Verify the API route and employee ID. |
| 500 Internal Server Error | Check application logs and exception details. |
| POST request fails | Verify the request JSON and required fields. |
Summary
In this article, we learned how to test an ASP.NET Core Web API using Swagger UI. We explored how to execute GET, POST, PUT, and DELETE requests and verify the API responses.
We also verified the CRUD operations directly in SQL Server to ensure that the API communicates correctly with the database.
With this, we have completed the initial implementation and manual testing of our Clean Architecture-based Employee Management API.
In the next article, we will explore how to write unit tests for the application using xUnit and mock dependencies using Moq.
Thanks
