Course contentsShow
← Software Engineering
Lesson 530 of 1,93316. Integration TestingFree lesson

Real vs Mocked Dependencies in Integration Tests

Understanding when to use real dependencies versus test doubles and the trade-offs between test realism and speed.

Real vs Mocked Dependencies in Integration Tests

You'll learn: When to use actual implementations versus test doubles in integration tests, and the critical trade-offs that determine which approach fits best.

The Central Question

Integration tests verify that modules work together correctly. But should your test hit a real database, call an actual HTTP service, or use mocked versions? This choice fundamentally shapes your test suite's characteristics.

The Two Approaches

Real dependencies mean using actual implementations—a real database, file system, or external service. These tests prove that your integration actually works in realistic conditions. The confidence is high, but these tests are slower, more fragile (network issues, database state), and harder to set up.

Mocked dependencies use test doubles (stubs, fakes, or mocks) to simulate external systems. These tests run fast, are reliable, and don't require complex infrastructure. However, they only prove your code works with the mock—not necessarily with the real dependency.

The Trade-Off Triangle

Think of it like rehearsing a play: practicing with the real stage and props (real dependencies) catches authentic problems but requires booking the theater. Rehearsing in a classroom with improvised props (mocked dependencies) is convenient and fast, but you might miss issues with the actual stage.

Choosing Your Strategy

Use real dependencies when:

  • Testing critical integration points
  • The dependency is fast and stable (in-memory database)
  • You've experienced bugs the mocks didn't catch

Use mocked dependencies when:

  • The real version is slow or expensive
  • Testing error scenarios (network failures)
  • The dependency is still being built
  • Running tests frequently during development

Key Takeaway: Integration tests work best with a mix—use real dependencies for critical paths and fast components, mocked dependencies for slow/flaky services and edge cases. The goal is maximum confidence with reasonable speed.