Skip to content
mentoor

Topic 2 of 5 · 4 min read

Integration tests at real boundaries

Goal: Choose which boundaries of a feature to test against real dependencies and which to fake, and justify each choice by the bug it would catch.

Test the places where your code meets something else (database, HTTP, queue) against the real thing, and fake only what you cannot run or do not own. Most production bugs sit at these boundaries, and unit tests with mocks cannot see them.

Check yourself · 1 of 3

A repository function is tested with a mocked database client that returns fixed rows. What bug can this test not catch?

Choose one answer

Why it matters

An agent will often write a repository test with a mocked database client. It passes, and the real query still fails: wrong column name, a missing index that makes it time out, a transaction that is never committed, a date stored in another time zone. A mock only returns what the author imagined. A real database returns what really happens.

How to choose

For each boundary of a feature, ask: what bug would this test catch, and would a fake catch it?

  1. Database. Use a real engine of the same type as production, started in a throwaway container (Testcontainers does this) or a disposable local instance. Run the real migrations. An in-memory substitute with different SQL behaviour is a fake, not a test of your queries.
  2. Your own HTTP API. Start the app and call it with a real HTTP client. Check status codes, response shape, validation errors and auth.
  3. Queues and jobs. Run the real broker in a container, publish a message and wait for the visible result, such as a row written. Test the retry and the duplicate message.
  4. Third-party services (payments, email, an AI model API). Fake these, ideally with a recorded contract or the provider's sandbox. You cannot control their uptime or cost. Add a small contract check so the fake stays honest.

Keep each test isolated: its own data, cleaned up or rolled back, no dependence on another test's rows.

Before and after

Weak: OrderRepository.findPaid() is tested with a mocked client returning two hand-written rows. It proves the function maps rows, nothing more.

Better: the test starts a real database, applies the migrations, inserts one paid and one unpaid order, calls findPaid() and expects only the paid one. Now a wrong WHERE clause or a renamed column fails the test.

What goes wrong

  • Mocking the boundary you meant to test. The suite is green and the first deploy fails.
  • Shared database state. Tests pass alone and fail together because they reuse rows.
  • Slow suites. Starting a container per test is wasteful; start one per run and reset data between tests.
  • Fakes that drift. The fake accepts input the real service rejects. Refresh recorded responses and check the real service on a schedule.

Examples

The mocked repository

const db = { query: vi.fn().mockResolvedValue([{ id: 1, status: "paid" }]) };
expect(await findPaid(db)).toHaveLength(1);

The query string is never executed. Run the same function against a containerised database with one paid and one unpaid row, and the WHERE status = 'paid' clause is finally tested.

A payment provider

server.use(http.post("https://api.pay.example/charges", () =>
  HttpResponse.json({ id: "ch_test", status: "succeeded" })));

Faking the provider is right: you cannot charge real cards in CI. Also test the failure path by returning a 402, so the checkout shows a clear message instead of crashing.

Key points

  • Test each boundary against the real thing when a fake would hide the bug you care about.
  • Use the same database engine as production and run the real migrations in tests.
  • Fake third-party services you cannot control, and keep the fake honest with a contract check.
  • Isolate every test's data so tests pass alone and together.
  • Ask for each test: what bug would this catch that a mock would miss?

Common mistakes

  • Mocking the database client and calling the result an integration test.
  • Using an in-memory database with different SQL rules from production.
  • Letting tests share rows, so the order of tests decides the result.
  • Faking every external service, including the ones the team owns and could run locally.

Check yourself · 2 of 3

Which dependency is the best candidate to fake in an integration test?

Choose one answer

Check yourself · 3 of 3

Integration tests pass one by one but fail when the whole suite runs. What is the most likely cause?

Choose one answer

Practice items, not counted towards your mastery.

Start here

More for this topic