Lesson 75 of 93 · 6 min read · Updated
Integration Tests Cross a Real Boundary: Testing the Full ASP.NET Core Pipeline
On this page
A unit test calls one method in memory. Nothing else runs: no routing, no model binding, no authorization, no database, no JSON serializer. That is why unit tests are fast, and it is also why they miss a whole class of bugs. An integration test sends a real HTTP request through the application and lets every layer do its job. This lesson shows what "crossing a real boundary" means in practice, using a small Books API and the tests that drive it.
Technical Requirements
- .NET 11 SDK
- xUnit 2.9.3
Microsoft.AspNetCore.Mvc.Testing11.0.0 (providesWebApplicationFactoryandTestServer)Microsoft.EntityFrameworkCore.Sqlite11.0.0- FluentAssertions 7.2.2 (the 7.x line is free; 8.x needs a commercial licence)
- Docker is not required. The tests use SQLite in-memory.
What Runs When the Test Sends a Request
The API under test is a minimal API with a /api/books group. It lists, creates, reads and deletes books, and the delete endpoint requires the AdminOnly policy. When an integration test calls POST /api/books, this chain runs, exactly as in production:
- routing matches the URL to an endpoint,
- model binding turns the JSON body into a
CreateBookRequest, - authentication and authorization middleware run,
- the endpoint validates input and talks to EF Core,
- EF Core translates LINQ to SQL and runs it against a relational database,
- the result is serialized back to JSON with the correct status code and headers.
A unit test of the endpoint method would skip all of this except the method body. The test class below runs the whole chain. It gets its HttpClient from a shared BooksApiFactory, which the next lessons explain in detail.
M09.IntegrationApi.Tests/HttpPipelineTests.cs
The test class shares one factory with other classes, so the data is shared too. ResetDatabaseAsync deletes all rows before every test. Each test therefore starts from an empty database, and the order in which tests run does not matter.
A Round Trip Through the Real Pipeline
The most valuable test in the class posts a book and then reads it back by id. It checks the HTTP contract (201 Created plus a Location header) and it checks that the data survives the trip through the database and the JSON serializer.
M09.IntegrationApi.Tests/HttpPipelineTests.cs
The last assertion looks trivial. It is not. The price is a decimal, SQLite has no native decimal type, and the value passes through a value converter on the way in and on the way out.
Pitfall: The Bug Only the Round Trip Caught
The first version of AppDbContext stored the price in SQLite with HasConversion<double>(). That worked on earlier .NET versions. In .NET 11, the double to decimal conversion returns the exact binary value of the double: (decimal)189.9d is no longer 189.9 but 189.90000000000000568434188608. After the upgrade every unit test still passed, because none of them touched the database. Only PostBook_ThenGetById_RoundTripsThroughTheRealPipeline failed.
The fix was to store money as a whole number of the smallest currency unit (kuruş, 1/100 of a lira) in a long column:
M09.IntegrationApi/Data/AppDbContext.cs
This is the core argument for integration tests: the bug lived between the code and the database, and only a test that crossed that boundary could see it.
Status Codes Are Part of the Contract
The same class checks the error paths. Each of these depends on something a unit test does not run.
M09.IntegrationApi.Tests/HttpPipelineTests.cs
The 404 test proves the route constraint {id:int} and the Results<Ok<BookResponse>, NotFound> return type work together. The 400 test proves the client receives a ValidationProblem body with field-level errors, which is what front-end code depends on. The 401 test proves RequireAuthorization("AdminOnly") is really attached to the endpoint. Remove that call by mistake and only this test fails.
These tests are still fast. The whole class runs in well under a second, because the server lives in memory and the database is SQLite in-memory. Integration tests do not have to be slow. They are slower than unit tests, so keep them for behaviour that needs the real boundary, and keep pure logic in unit tests.
Summary
- An integration test sends a real request through routing, model binding, authorization, EF Core and JSON serialization.
- A round-trip test (write, then read back) checks the data contract across the database and the serializer.
- In .NET 11, converting
doubletodecimalkeeps the exact binary value; store money as an integer of the smallest unit instead. - Status codes, headers and problem details are part of the API contract and deserve their own tests.
- Reset the shared database before each test so tests do not depend on run order.