Tutorials ASP.NET Core Complete Tutorial (ShopNest)

Integration Testing in ASP.NET Core

Learn Integration Testing in ASP.NET Core in our free ASP.NET Core Complete Tutorial (ShopNest) series. Step-by-step explanations, examples, and interview tips on Toolliyo Academy.

On this page
Integration Testing in ASP.NET Core — ShopNest
Article 55 of 75 · Module 7: Testing · ShopNest REST API End-to-End Testing
Target keyword: integration testing asp.net core · Read time: ~33 min · .NET: 8 / 9 · Project: ShopNest REST API End-to-End Testing

Introduction

Integration tests hit ShopNest API through WebApplicationFactory — real HTTP pipeline, real middleware, test database — catching wiring bugs unit tests miss.

After this article you will

  • Use WebApplicationFactory for in-memory test server
  • Call endpoints with HttpClient and assert responses
  • Configure SQLite or test SQL Server database
  • Authenticate test requests with JWT or test auth handler
  • Reset database state between tests with fixtures

Prerequisites

Concept deep-dive

public class ShopNestApiFactory : WebApplicationFactory<Program>
{
    protected override void ConfigureWebHost(IWebHostBuilder builder)
    {
        builder.ConfigureServices(services =>
        {
            var descriptor = services.SingleOrDefault(
                d => d.ServiceType == typeof(DbContextOptions<ShopNestDbContext>));
            if (descriptor != null) services.Remove(descriptor);

            services.AddDbContext<ShopNestDbContext>(options =>
                options.UseSqlite("DataSource=file:shopnest-test.db?mode=memory&cache=shared"));
        });
        builder.UseEnvironment("Testing");
    }
}

[Fact]
public async Task PostOrder_Returns201()
{
    var client = _factory.CreateClient();
    client.DefaultRequestHeaders.Authorization =
        new AuthenticationHeaderValue("Bearer", _factory.GetTestJwt());

    var response = await client.PostAsJsonAsync("/api/v1/orders", validOrder);
    response.StatusCode.Should().Be(HttpStatusCode.Created);
}

Unit vs integration: Unit = fast, isolated logic. Integration = slower, tests HTTP + DI + DB together.

Hands-on — ShopNest REST API End-to-End Testing

  1. ShopNest.Api.IntegrationTests project.
  2. WebApplicationFactory + SQLite in-memory shared connection.
  3. Seed products; POST order; GET order by id — full flow.
  4. IClassFixture<ShopNestApiFactory> for shared factory.
  5. Respawn or recreate DB per test class.

Common errors & best practices

  • Sharing mutable DB without reset — test order dependency failures.
  • Testing against production connection string — never.
  • Not replacing external APIs — use TestServer handler or WireMock.

Interview questions

Q: WebApplicationFactory?
A: Boots full app in memory for HttpClient tests without Kestrel network.

Q: When integration vs unit?
A: Unit for logic; integration for routes, auth, serialization, EF queries together.

Q: Test auth?
A: Test auth handler, pre-generated JWT, or WebApplicationFactory custom claims.

Summary

  • WebApplicationFactory enables realistic API tests
  • SQLite in-memory fast for CI pipelines
  • Integration suite catches DI and middleware bugs
  • Fixtures share expensive startup once per class

Previous: Unit Testing with xUnit and Moq
Next: Testing EF Core

FAQ

FluentAssertions?

Popular readable asserts — response.Should().BeSuccessful().

Testcontainers?

Spin real SQL Server in Docker for closer production parity.

Questions on this lesson 0

Sign in to ask a question or upvote helpful answers.

No questions yet — be the first to ask!

ASP.NET Core Complete Tutorial (ShopNest)
Course syllabus
Module 1: Foundations
Module 2: Entity Framework Core
Module 3: Dependency Injection & Middleware
Module 4: Authentication & Security
Module 5: Web API
Module 6: Advanced Architecture
Module 7: Testing
Module 8: Deployment & DevOps
Module 9: Real-World Projects
Module 10: Advanced Topics
Toolliyo Assistant
Ask about tutorials, ebooks, training, pricing, mentor services, and support. I use public site content only—not admin or internal tools.

care@toolliyo.com

Need callback? Share your details