Lesson 65 of 93 · 6 min read · Updated
Testing in Layered Architectures: Architecture Changes Boundaries, Not Principles
On this page
When a project moves from a single class library to an Onion, Clean or Hexagonal layout, many teams assume they need a new way of testing. They do not. The rules stay the same: test behavior, keep tests fast and isolated, and replace only what you do not control. What changes is where the boundaries are, and therefore where each kind of test starts and stops. This lesson introduces the layered sample project used in the rest of this section and shows how its boundaries shape the tests.
Technical Requirements
- .NET 11 SDK
- xUnit 2.9.3
- FluentAssertions 7.2.2 (the last version under the Apache 2.0 licence; 8.x is commercial)
- Moq 4.21.0
- Microsoft.Extensions.TimeProvider.Testing 11.0.0
- Microsoft.Extensions.DependencyInjection 11.0.0
- NetArchTest.Rules 1.3.2
The Layer Layout
The sample is a small order system. It uses one production project and represents the Onion layers with folders and namespaces. Each layer has one job, and dependencies only point inward.
The project file explains the choice. In a real system each layer would be a separate project, and the compiler would stop a wrong reference. In a single project the compiler cannot do that, so the rule is enforced with architecture tests instead.
M08.LayeredDdd/M08.LayeredDdd.csproj
Ports Point Inward, Adapters Live Outside
The Application layer needs to save orders, ask whether a customer is loyal and publish events. It does not know how those things happen. It declares what it needs as interfaces, called ports.
M08.LayeredDdd/Application/Ports/Ports.cs
The Infrastructure layer implements these ports. In the sample the adapters keep data in memory, which also makes them useful as lightweight fakes in tests.
M08.LayeredDdd/Infrastructure/InMemoryOrderRepository.cs
What the Boundaries Mean for Tests
Each boundary in the layout is also a natural place to start or stop a test. The principles do not move, but the scope of each test follows the layers:
| Layer | What the tests check | What they need |
|---|---|---|
| Domain | Business decisions: rules, state transitions, invariants, events | Plain C#. No mocks, no container, no database |
| Application | Use cases: orchestration, policies, port interactions, error paths | Fakes or mocks for the ports, a fake clock |
| Application + container | DI registrations and the command pipeline | A real ServiceCollection |
| Whole assembly | Dependency rules between layers | NetArchTest |
Because the Domain layer depends on nothing, its tests are the cheapest to write and the fastest to run. Because the Application layer talks to the outside world only through ports, its tests can swap each port for an in-memory adapter or a mock without changing production code. Infrastructure adapters, the parts that touch real databases and queues, belong to integration tests and are out of scope for this section.
Enforcing the Direction
A layering rule that lives only in a wiki page will be broken sooner or later. Somebody adds a using for an Infrastructure type inside a use case, the build passes, and the layers start to blur. An architecture test turns the rule into something that fails the build.
M08.LayeredDdd.Tests/Architecture/ArchitectureTests.cs
The reason message lists the types that broke the rule, so a failure tells you exactly which file to fix. Architecture tests get their own lesson at the end of this section.
A common mistake is to treat the layer structure as a reason to mock everything at every boundary. The boundaries tell you where tests can start and stop. They do not tell you to replace the domain model with mocks in application tests. The real Order aggregate is used in every application test in this section, because it is fast, deterministic and part of the behavior being tested.
Summary
- Layered architectures do not change testing principles; they change where test boundaries sit.
- The Domain layer depends on nothing, so domain tests are plain C# with no mocks.
- The Application layer reaches the outside world only through ports, so its tests replace ports with in-memory adapters or mocks.
- When layers share one project, architecture tests enforce the dependency direction that separate projects would enforce at compile time.
- Do not mock the domain model in application tests; use the real aggregate.