C# Interview Question #181
What is integration testing in ASP.NET Core?
Testing, Security, APIs & Reliability Senior Advanced
Quick Interview Answer
Integration testing verifies that multiple application components work together correctly. Unlike a unit test, it may exercise routing, middleware, model binding, dependency injection, authentication, database access, and HTTP responses together.
Detailed Explanation
Integration testing verifies that multiple application components work together correctly. Unlike a unit test, it may exercise routing, middleware, model binding, dependency injection, authentication, database access, and HTTP responses together.
ASP.NET Core provides WebApplicationFactory<TEntryPoint> through Microsoft.AspNetCore.Mvc.Testing for hosting an application in a test environment.
Integration tests are valuable for critical API endpoints and infrastructure boundaries because they can catch configuration and wiring problems that isolated unit tests cannot detect.
Code Example
public class ProductsApiTests :
IClassFixture<WebApplicationFactory<Program>>
{
private readonly HttpClient _client;
public ProductsApiTests(
WebApplicationFactory<Program> factory)
{
_client = factory.CreateClient();
}
[Fact]
public async Task GetProducts_ReturnsSuccess()
{
var response =
await _client.GetAsync("/api/products");
response.EnsureSuccessStatusCode();
}
}