ASP.NET Clean Architecture: Test the API Using Swagger

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

  1. Open the CleanArchitectureDemo solution in Visual Studio 2022.
  2. Set the API project as the startup project.
  3. Ensure the SQL Server connection string is configured correctly.
  4. Make sure database migrations have been applied.
  5. Press F5 or click the Run button.

Once the application starts, open Swagger UI in your browser.

Clean architecture by uncle blob

Test Employee API Endpoints Using Swagger

Your Employee Management API contains the following CRUD endpoints.
HTTP MethodEndpointDescription
GET/api/employeesGet all employees
GET/api/employees/{id}Get employee by ID
POST/api/employeesCreate an employee
PUT/api/employees/{id}Update an employee
DELETE/api/employees/{id}Delete an employee

5.1 Test GET – Retrieve All Employees
  1. Expand the GET /api/employees endpoint.
  2. Click the Try it out button.
  3. 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 CodeDescription
200 OKRequest completed successfully.
201 CreatedA new employee was created.
204 No ContentRequest succeeded without returning a response body.
400 Bad RequestInvalid request data.
404 Not FoundEmployee record does not exist.
500 Internal Server ErrorUnexpected 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

IssuePossible Solution
Swagger page not foundVerify Swagger middleware configuration.
Unable to connect to databaseCheck the SQL Server connection string.
404 Not FoundVerify the API route and employee ID.
500 Internal Server ErrorCheck application logs and exception details.
POST request failsVerify 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

Kailash Chandra Behera

I am an IT professional with over 12 years of experience in the full software development life cycle for Windows, services, and web-based applications using Microsoft .NET technologies.

Please Select Embedded Mode To Show The Comment System.*

Previous Post Next Post

نموذج الاتصال