The BackendDeeper

Kayavak / The Backend / BE·54 Backend Testing / Deeper

Deeper · The Backend

Backend Testing

The questions the chapter doesn't stop for: why this exists, the model to keep in your head, what changes when an assumption does, how sure we are of each claim, and what's still open. Read it after the chapter; back to the chapter.

Why does this exist?

Automated unit testing as most developers know it started with small frameworks: Kent Beck's SUnit for Smalltalk (1990s), then JUnit, which Beck and Erich Gamma wrote in 1997, famously starting on a flight to a conference. The "xUnit" pattern spread to every language. Beck's Test-Driven Development (2002) argued for writing the test first, so design follows from use. Mock Roles, not Objects (Steve Freeman, Nat Pryce and others, 2004) shaped how test doubles are used, and warned against mocking things you don't own.

Integration tests long meant shared, fragile test databases. Testcontainers (2015) made it practical to start a real PostgreSQL, Kafka or Redis in a container for each test run, so tests check against the real thing rather than an in-memory imitation. Property-based testing (QuickCheck, 2000) and mutation testing added ways to find cases nobody thought to write.

The mental model

Try it yourself first: write your own model in the Mental Model Explorer, then compare.

Tests are executable claims about behaviour that let you change code without fear. Test at the cheapest level that can catch a bug, against the real thing where fakes would lie, and judge the suite by the bugs it catches, not its coverage.

Question Answer
What is it? Automated checks at unit, integration and end-to-end levels for backend code
Problem it solves Changing and deploying code frequently without breaking behaviour
Parts JUnit, assertions, test doubles, Spring test slices, Testcontainers, contract tests, property tests, mutation testing
How it works Arrange a known state, act through a public interface, assert on the observable result
Assumptions Tests are deterministic and independent; fakes behave like the real dependency
When they change Shared state between tests, clocks and randomness make tests flaky; an in-memory database accepts SQL the real one rejects
Where it fails Over-mocked tests that pass while production breaks; slow suites that people stop running
Related ideas Frontend testing (The Web Frontend, ch26); the Testing volume

What if?

What if you tested the repository against an in-memory database instead of PostgreSQL?

Reason it through, then open

Tests would be fast, and would miss everything that differs: SQL dialect, locking and isolation behaviour, constraints, JSON operators, query plans. A migration that works in H2 can fail in production. Testcontainers runs the real database for integration tests, at the cost of a few seconds of start-up.

What if every collaborator in a unit test were mocked?

Reason it through, then open

The test would check that your code calls its mocks in a particular way: it would pin the implementation, failing on harmless refactors, and still pass if the real collaborators behaved differently from the mocks. Mock at boundaries you own and that are expensive or non-deterministic (a payment gateway, a clock); use real objects for the rest.

Where the boundary is

Aspect Mock Fake
Is A double you program with expectations A working lightweight implementation (in-memory repository)
Verifies Interactions: was this called? State: what's there afterwards?
Breaks when The implementation changes how it calls things The fake drifts from the real behaviour
Prefer At external boundaries For your own interfaces

How sure are we?

Claim How it's known Where from
JUnit was written by Beck and Gamma in 1997 Historical Their accounts
Coverage alone weakly predicts test effectiveness Measured Inozemtseva and Holmes, 2014
TDD improves design and defect rates Active research Studies give mixed results; effects depend on context and experience

Still open

How to keep large suites fast and reliable (test selection by changed code, flaky-test quarantine) is an engineering problem every big codebase is still solving, and AI-written tests add the question of whether a test encodes intended or merely current behaviour.

What would change your mind? If your suite caught most regressions before production, it's doing its job whatever its coverage number. Track escaped bugs, not just coverage.

Recall

Why test against a real database with Testcontainers?

In-memory substitutes differ in SQL, locking, constraints and plans, so tests can pass while production fails.

What's the risk of mocking every collaborator?

Tests pin the implementation and can pass even when real collaborators behave differently.

What's the difference between a mock and a fake?

A mock verifies interactions; a fake is a working simplified implementation checked by state.

Is TDD proven to reduce defects?

Evidence is mixed; it depends on context and experience.

Reflect

Be honest with yourself; this stays in your browser. Anything you can't tick yet is what to revisit.