Introduction
Before writing professional tests, we need to define the boundaries of the application and the testing strategy. If the code under test is poorly organized, tests quickly become complicated, slow, and fragile. Therefore, this first lesson is not only about creating several project files. We will also plan the responsibilities of the e-commerce order system, the dependency direction between layers, and the test level that should verify each behavior.
Throughout this series, we will build a simple but realistic e-commerce order API named OrderFlow. The application will eventually support product selection, order creation, stock validation, total calculation, and persistent order storage. We will not implement every feature in this lesson. Instead, we will create a reliable foundation for the following lessons.
A good testing strategy does not only define “how many tests we will write”; it defines which behavior is protected at which level.
The structure we will create
Clean Architecture aims to separate business rules from external details. Domain rules should remain stable even if the database, HTTP framework, or third-party libraries change. We will organize the solution into four production projects and two test projects:
| Project | Responsibility | Dependency direction |
|---|---|---|
| OrderFlow.Domain | Entities, value objects, and core business rules | No external project dependency |
| OrderFlow.Application | Use cases, service contracts, DTOs, and application workflows | References Domain |
| OrderFlow.Infrastructure | EF Core, repositories, and external service implementations | References Application and Domain |
| OrderFlow.Api | HTTP endpoints, middleware, and DI configuration | References Application and Infrastructure |
| OrderFlow.Application.Tests | Unit tests for business rules and application services | References Application |
| OrderFlow.Api.IntegrationTests | Tests for the API, DI, middleware, and infrastructure working together | References Api |
The dependency direction is important. The Domain project should not know about ASP.NET Core, Entity Framework Core, or Moq. The Application layer defines the contracts it needs instead of knowing how data is stored. Infrastructure implements those contracts. The API exposes the application through an HTTP boundary.
Creating the solution and projects
First, verify that the .NET 10 SDK is installed:
dotnet --version
Create a working directory and initialize the solution:
mkdir OrderFlow cd OrderFlow dotnet new sln --name OrderFlow
With the .NET 10 SDK, the solution may be created in the newer .slnx format. If your team uses another SDK version, you may see the traditional .sln format instead. This does not change how project references work.
Now create the production projects:
mkdir src tests dotnet new classlib --name OrderFlow.Domain --output src/OrderFlow.Domain --framework net10.0 dotnet new classlib --name OrderFlow.Application --output src/OrderFlow.Application --framework net10.0 dotnet new classlib --name OrderFlow.Infrastructure --output src/OrderFlow.Infrastructure --framework net10.0 dotnet new webapi --name OrderFlow.Api --output src/OrderFlow.Api --framework net10.0 --use-controllers
Create the test projects as well:
dotnet new xunit --name OrderFlow.Application.Tests --output tests/OrderFlow.Application.Tests --framework net10.0 dotnet new xunit --name OrderFlow.Api.IntegrationTests --output tests/OrderFlow.Api.IntegrationTests --framework net10.0
Add the projects to the solution:
dotnet sln add src/OrderFlow.Domain/OrderFlow.Domain.csproj dotnet sln add src/OrderFlow.Application/OrderFlow.Application.csproj dotnet sln add src/OrderFlow.Infrastructure/OrderFlow.Infrastructure.csproj dotnet sln add src/OrderFlow.Api/OrderFlow.Api.csproj dotnet sln add tests/OrderFlow.Application.Tests/OrderFlow.Application.Tests.csproj dotnet sln add tests/OrderFlow.Api.IntegrationTests/OrderFlow.Api.IntegrationTests.csproj
Configuring project references correctly
Next, define the allowed dependency directions. In .NET 10, the dotnet reference add command can add project references:
dotnet reference add ../OrderFlow.Domain/OrderFlow.Domain.csproj --project src/OrderFlow.Application/OrderFlow.Application.csproj dotnet reference add ../OrderFlow.Domain/OrderFlow.Domain.csproj --project src/OrderFlow.Infrastructure/OrderFlow.Infrastructure.csproj dotnet reference add ../OrderFlow.Application/OrderFlow.Application.csproj --project src/OrderFlow.Infrastructure/OrderFlow.Infrastructure.csproj dotnet reference add ../OrderFlow.Application/OrderFlow.Application.csproj --project src/OrderFlow.Api/OrderFlow.Api.csproj dotnet reference add ../OrderFlow.Infrastructure/OrderFlow.Infrastructure.csproj --project src/OrderFlow.Api/OrderFlow.Api.csproj dotnet reference add ../../src/OrderFlow.Application/OrderFlow.Application.csproj --project tests/OrderFlow.Application.Tests/OrderFlow.Application.Tests.csproj dotnet reference add ../../src/OrderFlow.Api/OrderFlow.Api.csproj --project tests/OrderFlow.Api.IntegrationTests/OrderFlow.Api.IntegrationTests.csproj
Verify that the solution works:
dotnet restore dotnet build dotnet test
At this point, the test projects may contain only the template test. That is expected. The important goal is that the solution can already be restored, built, and tested with a single command sequence.
Preparing the test projects
In the application unit test project, we will use Moq to isolate dependencies in later lessons. We will use FluentAssertions for readable assertions. Add the packages to the unit test project:
dotnet package add Moq --project tests/OrderFlow.Application.Tests/OrderFlow.Application.Tests.csproj dotnet package add FluentAssertions --project tests/OrderFlow.Application.Tests/OrderFlow.Application.Tests.csproj
API integration tests will start the application entry point and use a test server, so they need Microsoft.AspNetCore.Mvc.Testing:
dotnet package add Microsoft.AspNetCore.Mvc.Testing --project tests/OrderFlow.Api.IntegrationTests/OrderFlow.Api.IntegrationTests.csproj dotnet package add FluentAssertions --project tests/OrderFlow.Api.IntegrationTests/OrderFlow.Api.IntegrationTests.csproj
Do not add these packages to every project without a reason. For example, adding Moq to the Domain project makes production domain code aware of a testing tool and weakens the layer boundary.
The boundary between unit and integration tests
A unit test verifies a small piece of code in isolation from its external dependencies. For example, if OrderService uses IProductRepository to check stock, a unit test can provide a mock or fake repository instead of a real database. The test then measures whether the service makes the correct decision, not whether the database happens to be available.
An integration test verifies multiple components working together in a realistic environment. For an ASP.NET Core API, this can include application startup, dependency injection registration, routing, middleware, model binding, and HTTP response generation. In later lessons, we will create a test server with WebApplicationFactory<Program>.
| Behavior | Recommended test | Reason |
|---|---|---|
| Invalid quantity is rejected | Unit test | It is a fast business rule |
| Total amount is calculated correctly | Unit test | No external system is required |
| POST endpoint returns 201 | Integration test | HTTP pipeline and controller are tested together |
| Order is persisted | Integration test | EF Core and database behavior are verified |
| A repository method is called with expected data | Unit test | Application collaboration is tested |
When a behavior can be verified with either a unit or integration test, prefer the unit test in general. However, keep a small number of integration tests for critical user flows. Repeating every data permutation at the integration level makes the suite slower and debugging harder.
Planning the testing pyramid
The testing pyramid recommends many fast unit tests at the bottom, fewer integration tests in the middle, and a limited number of end-to-end tests at the top. In this course, we will aim for the following general distribution:
- Unit tests: Domain rules, application services, failures, and boundary cases.
- Integration tests: API endpoints, DI configuration, repositories, and database access.
- End-to-end scenarios: A few critical flows from order creation to persistence.
This distribution is not a rigid law. We will not increase test count only to improve a coverage percentage. The goal is to protect important behavior reliably when the code changes.
Creating the first domain contract
To confirm that the project structure works, add a simple domain entity. Create OrderFlow.Domain/Orders/Order.cs:
namespace OrderFlow.Domain.Orders; public sealed class Order { public Guid Id { get; private set; } public decimal Total { get; private set; } public DateTime CreatedUtc { get; private set; } private Order() { } public Order(Guid id, decimal total, DateTime createdUtc) { if (id == Guid.Empty) throw new ArgumentException("Order id cannot be empty.", nameof(id)); if (total <= 0) throw new ArgumentOutOfRangeException(nameof(total)); Id = id; Total = total; CreatedUtc = createdUtc; } }
This is not yet a complete domain model. The purpose is to confirm that the Domain layer can compile without ASP.NET Core or EF Core. The constructor rules will later be verified directly with unit tests.
Common mistakes
- Referencing every project from every other project: This makes the dependency graph impossible to control. Add a reference only when the responsibility truly requires it.
- Adding framework dependencies to the Domain layer: Domain rules should not require starting an ASP.NET Core host to be tested.
- Using integration tests like unit tests: Sending an HTTP request for a simple calculation is unnecessarily slow.
- Not referencing the real production project: Tests should execute the actual application code, not copied versions of classes.
- Connecting tests to the development database: Tests must be isolated and repeatable.
- Focusing only on coverage percentage: High coverage does not provide real confidence if assertions are weak or meaningless.
Short practice exercise
- Create the OrderFlow solution and all six projects.
- Add only the permitted project references.
- Run dotnet build and dotnet test.
- Add the Order entity.
- Confirm that the Domain project builds without ASP.NET Core or EF Core packages.
- Write a three-item test strategy: choose two unit test topics and one integration test topic.
A suitable example plan is: a unit test for an invalid total, a unit test for the order service’s repository collaboration, and an integration test verifying that POST /api/orders returns a successful response.
Conclusion
In this lesson, we created the foundation of a real e-commerce order application. We separated the Domain, Application, Infrastructure, and API layers, then prepared separate projects for unit and integration tests. The most important lesson is to treat testing not as an activity performed at the end of development, but as a development process planned together with architectural decisions.
In the next lesson, we will organize the test project in more detail, apply the Arrange-Act-Assert pattern with xUnit, and write our first domain unit tests.
Sources
- https://learn.microsoft.com/en-us/aspnet/core/tutorials/first-web-api?view=aspnetcore-10.0
- https://learn.microsoft.com/en-us/aspnet/core/fundamentals/dependency-injection?view=aspnetcore-10.0
- https://learn.microsoft.com/en-us/dotnet/core/tools/dotnet-sln
- https://learn.microsoft.com/en-us/dotnet/core/tools/dotnet-add-reference
- https://learn.microsoft.com/en-us/aspnet/core/test/integration-tests?view=aspnetcore-10.0
- https://xunit.net/docs/getting-started/v3/getting-started
- https://github.com/devlooped/moq
- https://fluentassertions.com/introduction