Integration Testing
Overview
Integration testing validates interactions between components such as API layers, databases, queues, and external services.
Scope
- Service to database integration
- Service to service contract behavior
- Message publication and consumption
- Authentication and authorization flows
sequenceDiagram
participant Test
participant API
participant DB
Test->>API: POST /orders
API->>DB: insert order
DB-->>API: success
API-->>Test: 201 Created
Practical Example
public class OrdersApiTests : IClassFixture<ApiTestFactory>
{
private readonly HttpClient _client;
public OrdersApiTests(ApiTestFactory factory)
{
_client = factory.CreateClient();
}
[Fact]
public async Task CreateOrder_ReturnsCreated()
{
var payload = new { customerId = "cust-1", total = 149.90m };
var response = await _client.PostAsJsonAsync("/orders", payload);
Assert.Equal(HttpStatusCode.Created, response.StatusCode);
}
}
Integration tests run against real dependencies in controlled environments.
Production Guidance
- Keep test data isolated per run
- Validate API contracts and persistence side effects
- Run in CI with deterministic environment setup
Common Pitfalls
- Sharing state between tests
- Depending on external unstable services
- Running integration suites too infrequently
Key Takeaways
- Integration tests catch boundary issues missed by unit tests.
- Deterministic environments reduce flaky behavior.
- Contract-focused assertions improve cross-service reliability.