ASP.NET Core Interview Question #202

What is integration testing in ASP.NET Core?

Testing, Deployment & Hosting Senior Advanced

Quick Interview Answer

Integration testing verifies that multiple application components work together. In ASP.NET Core, an integration test can start a test host and send real HTTP requests through routing, middleware, filters, controllers, serialization, and configured services.

Detailed Explanation

Integration testing verifies that multiple application components work together. In ASP.NET Core, an integration test can start a test host and send real HTTP requests through routing, middleware, filters, controllers, serialization, and configured services.

Integration tests are slower than unit tests but can detect problems that isolated tests miss, such as incorrect routes, dependency registrations, authorization behavior, or serialization settings.

A balanced test suite normally uses many focused unit tests and a smaller number of high-value integration tests.

Code Example

public class ProductsTests :
    IClassFixture<WebApplicationFactory<Program>>
{
    private readonly HttpClient _client;

    public ProductsTests(WebApplicationFactory<Program> factory)
    {
        _client = factory.CreateClient();
    }

    [Fact]
    public async Task GetProducts_ReturnsSuccess()
    {
        var response = await _client.GetAsync("/api/products");
        response.EnsureSuccessStatusCode();
    }
}