Vladimir Khorikov offers a simple rule for deciding what to mock, and explains the trade-offs every test suite must navigate.

Key Takeaways

  • Khorikov only allows mocking dependencies that other systems can also access. Your application's own database should be tested against a real instance.
  • According to a reading note on the book, good tests prevent regressions, survive refactoring, give fast feedback, and are easy to maintain. The first three cannot all be maximized at once.
  • The book is aimed at readers who already know the basics of unit testing. The examples are written in C#, but apply to any language.

Only mock external dependencies shared with other systems. Vladimir Khorikov, author of Unit Testing: Principles, Practices, and Patterns, proposed this rule in 2020 on his blog, Enterprise Craftsmanship. When a Forward Deployed Engineer (FDE) has just finished writing a service that connects a customer's system to your company's product, this one sentence can completely transform the entire test suite you're about to deliver.

The reason lies in how delivery works. Once you leave the customer's site, it's the customer's own engineers who modify that integration code. Only the test suite can tell them what they just broke. If the tests are full of mocks and turn red on every refactor—even when there's no bug at all—the suite will quickly be switched off. At that point, it's as good as nonexistent.

Whether to mock depends on who else uses the dependency

Khorikov divides out-of-process dependencies into two categories. Managed dependencies are accessible only through your application, such as the service's own database. Unmanaged dependencies can also be accessed by other applications. From this he derives the rule: use real instances for managed dependencies, and mock only unmanaged dependencies.

Take a service at a distribution company as an example. It receives orders, stores them in its own Postgres database, and then pushes them to an ERP system's API, which the customer's finance and warehouse teams also read from.

Postgres is a managed dependency, because nothing outside your service touches it. The ERP is an unmanaged dependency, because the calls you send it form a contract that other systems rely on.

Applied here, the rule means: tests run against a real Postgres, while the ERP is mocked at the boundary to verify that outgoing requests are correct. Now suppose that a few months later, the customer's team renames a column during a migration.

If the database had been mocked, the tests would still be green while production is already broken. Running against real Postgres, CI would turn red immediately. Mocking the ERP remains reasonable: the test only needs to prove that the service honors its contract with the outside world.

No test suite can have all three

The next question is how to distinguish good tests from bad ones. According to a reading note summarizing the book, Khorikov proposes four pillars: protection against regressions, resistance to refactoring, fast feedback, and maintainability. Crucially, the first three are mutually exclusive. You can only pick two of the three—a trade-off similar to the CAP theorem.

Back to the order service. An end-to-end test calling a real ERP sandbox catches many bugs and is unaffected by changes to the code's internal structure, but it's slow. Small tests that mock everything run extremely fast, yet are too tightly coupled to implementation details—extract a function and they fail. The latter is exactly what customer teams hate most.

Understanding this trade-off means you stop searching for one "correct" type of test. For the order service, a sensible allocation is: plenty of fast tests covering pure business logic, plus a small number of integration tests running against a real database.

In the book's table of contents, Manning lists verifying the whole system with integration tests, avoiding anti-patterns, and refactoring tests in sync with production code, among other topics.

Who should read it, and in what order

Manning published the book in January 2020, at 304 pages. Khorikov describes it on his personal website as a comprehensive guide to unit testing, and argues that programming rules should be derived from first principles.

Manning explicitly states that the book is written for people who already know the basics of unit testing. If you've never written a test, learn a testing framework first and come back. The examples are written in C#, but the publisher says they apply to any language.

If a customer's schedule leaves you with fragmented time, read it in this order. Start with the blog post "When to Mock"—one evening is enough.

Then read the section on the four pillars, since these are the criteria by which you'll score every test. Finally, read the chapters on mocks and integration tests, with the code you're currently writing open alongside.

Manning promises to help readers improve their existing test suites with modern practices, which fits the FDE reality well. You rarely get to write tests from scratch; more often, you inherit a customer codebase already full of slow, brittle, mock-heavy tests.

Turn this book into an interview advantage

Most candidates write only one line on their resume: "wrote unit tests." You can be more specific: integration tests run against a real database, mocks are placed only at API boundaries shared with other systems, and the test suite remains stable when the customer's team refactors after delivery.

That one line shows the hiring manager that you understand not just syntax, but delivery.

When asked in an interview about your testing strategy for integrated systems, answer in terms of trade-offs, not coverage percentages. Explain which two of the three pillars each layer of testing chooses, and why. This is exactly the first-principles thinking Khorikov advocates.

When you leave the site, your code stays behind. The test suite will answer the questions the customer's team hasn't yet thought to ask.

Was this article helpful?

Thanks for your feedback!

4 sources

Next on the roadmap · Phase 6: Measuring Value. In this generative AI evals course, only 14 of the 47 minutes cover LLMs. The rest is still worth watching: logging every run and versioning your data—habits that apply to any tech stack.