Why the integration code you hand over needs fewer mocks
Vladimir Khorikov offers a compact rule for what to mock, and explains the trade-off every test suite has to make.
In brief
- Khorikov allows mocks only for dependencies that other systems can also reach. The application's own database should be tested with a real instance.
- According to one summary of the book, a good test protects against regressions, resists refactoring, gives fast feedback and is easy to maintain. The first three cannot all be maximized at once.
- The book is for readers who already know basic unit testing. Its examples are in C# but apply to any language.
Mock only the external dependencies that other systems share. Vladimir Khorikov, author of Unit Testing: Principles, Practices, and Patterns, set out this rule on his blog, Enterprise Craftsmanship, in 2020. For a forward deployed engineer who has just finished a service connecting a client’s systems to the company’s product, that one sentence can reshape the entire test suite about to be handed over.
The reason lies in how handovers work. Once you leave the client site, the client’s engineers will be the ones changing that integration code. The test suite is the only thing that tells them what they have just broken. A suite full of mocks, which turns red on every refactor even when there is no bug, will soon be switched off. At that point it might as well not exist.
Whether to mock depends on who else uses the dependency
Khorikov divides out-of-process dependencies into two kinds. A managed dependency is reachable only through your application, such as a service’s own database. An unmanaged dependency is one that other applications can reach too. From this he derives the rule: use real instances for managed dependencies, and mock only unmanaged ones.
Take a service built for a distribution company. It receives orders, stores them in its own Postgres database, then pushes them to the API of an ERP system that the client’s accounting and warehouse teams also read.
Postgres is managed, because nothing outside your service touches it. The ERP is unmanaged, because the calls you send it form a contract that other systems depend on.
Applied here, the rule means tests run against a real Postgres and mock the ERP at the boundary, checking that the outgoing requests are correct. Now suppose that a few months later the client’s team renames a column in a migration.
If you had mocked the database, the tests stay green while production breaks. Run against a real Postgres, they go red in CI straight away. Mocking the ERP still makes sense: the test only needs to prove that the service honors its contract with the outside world.
No test suite gets all three at once
The next question is how to tell a good test from a bad one. According to one set of notes summarizing the book, Khorikov names four pillars: protection against regressions, resistance to refactoring, fast feedback and maintainability. The crucial point is that the first three are mutually exclusive. You get to pick two of the three, a trade-off similar to the CAP theorem.
Back to the order service. An end-to-end test that calls a real ERP sandbox catches many bugs and survives changes to the internal structure of the code, but it is slow. A small test that mocks everything runs very fast, yet it is tied so closely to implementation details that it breaks as soon as you extract a function. That second kind is the one the client’s team will hate most.
Once you understand the trade-off, you stop hunting for a single “correct” type of test. For the order service, a sensible split is many fast tests for pure business logic, plus a small number of integration tests running on a real database.
In the book’s table of contents, Manning lists using integration tests to verify the whole system, alongside avoiding anti-patterns and refactoring tests in step with production code.
Who should read it, and in what order
Manning published the book in January 2020, at 304 pages. On his own site Khorikov calls it a comprehensive guide to unit testing, and argues that programming rules should be derived from first principles.
Manning states plainly that the book is written for people who already know the basics of unit testing. If you have never written a test, learn a framework first and come back later. The examples are in C#, though the publisher says they apply to any language.
If client schedules break your time into fragments, this order works. Start with the blog post “When to Mock”, which takes one evening.
Then read the section on the four pillars, since those are the criteria you will use to grade each test. Finish with the chapters on mocks and integration tests, with the code you are currently writing open alongside.
Manning promises to help readers improve existing test suites with modern practices, and that promise matches FDE reality. You rarely get to write tests from scratch. More often you inherit a client repository that already has slow, brittle tests with mocks everywhere.
Turning the book into an interview advantage
Most candidates put a single line on their CV along the lines of “wrote unit tests”. You can be more specific: integration tests running on a real database, mocks placed only at API boundaries shared with other systems, and a suite that stayed stable when the client’s team refactored after handover.
That line shows a hiring manager that you understand handovers as well as syntax.
In an interview, if asked about a testing strategy for an integration system, answer in terms of trade-offs and leave coverage percentages aside. Explain which two of the three pillars you chose for each layer of tests, and why. That is the first-principles thinking Khorikov champions.
When you leave the site, your code stays behind. The test suite will answer, on your behalf, the questions the client’s team has not yet thought to ask.
Was this article useful?
Thanks for the feedback!