Quick revision
The short answer
I would decide with defect data rather than opinions: look at which kind of test actually catches which defects in our systems. Container-based tests are now cheap enough to replace most mocks of our own databases and brokers, while mocks remain right for slow, costly or uncontrollable boundaries such as third-party APIs. What I write down is a short standard with examples that says which test type to use where, allows team variation within it, and is reviewed as the tools change.
What to remember
In this answer, we saw that the mocking camp and the container camp optimise for different defects, so the decision should come from our own defect data. Cheap containers now make real databases and brokers the default for our own infrastructure, while mocks stay right for slow, costly or uncontrollable third-party boundaries. We wrote down a short placement standard with examples and guardrails, allowed teams to vary with evidence, and gave it an owner and a yearly review so the direction stays valid as tools change.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
I would decide with defect data rather than opinions: look at which kind of test actually catches which defects in our systems. Container-based tests are now cheap enough to replace most mocks of our own databases and brokers, while mocks remain right for slow, costly or uncontrollable boundaries such as third-party APIs. What I write down is a short standard with examples that says which test type to use where, allows team variation within it, and is reviewed as the tools change.
Background to this question
A ten-year direction is not a choice of library; it is a set of principles that outlives several libraries. When two camps argue, mocking-heavy unit tests against real infrastructure in containers, each is usually right about something, and a senior engineer's job is to find the deciding evidence and write a standard that teams can apply without asking. In this answer, we will understand what each camp gets right, decide using defect data, look at how cheap containers changed the balance, and then write the standard, including what may vary between teams and how it stays current.
1What Each Camp Gets Right
The mocking camp values speed and focus. A unit test with mocks runs in milliseconds, points exactly at the broken class, and works without Docker. Thousands of such tests can run on every save.
The container camp values realism. A test against a real PostgreSQL in a container catches the bugs mocks cannot: wrong SQL, a missing index that makes a query time out, a mapping that fails on a null column, a Kafka consumer that commits offsets in the wrong place. Those are the bugs that tend to reach production.
Notice that they are not really disagreeing about the same tests. They are optimising for different kinds of defects. So the decision should start from which defects we actually have.
2Deciding With Defect Data
I would take the last 12 months of production incidents and escaped defects, perhaps 150 of them, and for each one ask two questions: where was the bug, and what is the cheapest test that would have caught it? This takes a few days with the teams and gives a picture that no argument can.
A typical result for a Spring and PostgreSQL estate looks like the table below. The shape matters more than the exact numbers: logic errors are caught well by unit tests, but data access, integration and configuration errors dominate the escapes, and unit tests with mocks cannot see them by design.
| Defect kind | Share | Cheapest test that catches it |
|---|---|---|
| SQL, mapping, migrations | 35 percent | Testcontainers data access test |
| Integration between our services | 25 percent | contract test or container test |
| Business logic | 20 percent | plain unit test |
| Configuration and wiring | 12 percent | Spring slice or boot test |
| Third-party API behaviour | 8 percent | WireMock or contract test of the adapter |
3How Cheap Containers Changed the Balance
Ten years ago, testing against a real database meant a shared server, slow setup and tests that interfered with each other, so mocking repositories was a reasonable trade. Today Testcontainers starts a PostgreSQL in a few seconds, Spring Boot's @ServiceConnection wires it with no configuration, and a singleton container can be shared by a whole test run. Running 400 data access tests against a real database costs perhaps two minutes in CI.
That changes the economics. For infrastructure we own and can run in a container, such as databases, brokers and caches, a real instance is now cheaper than writing and maintaining mocks that only approximate it. Mocks keep their place where a real instance is slow, costly or not ours to control: a payment provider charges per call, a partner API has rate limits and test data we cannot set up, an email service sends real mail.
4What We Write Down
The standard itself is short, two pages at most, because nobody follows a long one. It states the principle first: choose the cheapest test that would catch the defects we actually have. Then it gives a placement rule for each kind of code, with a short example from our own code base for each, so engineers can copy a good pattern.
| Code | Default test | Doubles allowed |
|---|---|---|
| Domain logic, value objects | plain unit test | none needed |
| Application services | unit test at the service boundary | fakes; mocks for external ports |
| Repositories, queries, migrations | Testcontainers slice test | none, real database |
| Messaging producers and consumers | Testcontainers with real Kafka | none, real broker |
| Third-party API adapters | WireMock or contract test | recorded or stubbed HTTP |
| Cross-service journeys | few end-to-end tests | shared environment |
It also says what it is not: it is not a coverage target and not a ban on mocks. Mocks are recommended at third-party boundaries and for verifying effects such as sent messages; they are discouraged for our own databases, brokers and value objects.
5Allowing Team Variation
A single rule for 60 teams fails, because their systems differ. A team building a pricing engine with complex rules and little I/O will rightly have many pure unit tests. A team building a data pipeline will have mostly container tests. So the standard sets defaults and leaves room: teams may deviate when they can explain it with their own defect data, and they write the reason in their repository's testing notes.
What does not vary are a few guardrails: data access is always tested against a real database, third-party APIs are always behind an adapter of our own, and suites must stay within a time budget, for example under 15 minutes in CI, so realism does not cost us fast feedback.
6Keeping a Ten-Year Direction Current
Tools will change during those ten years. Testcontainers itself may be replaced, CI runners may change, and new kinds of infrastructure will appear. So the standard has an owner, usually the platform or developer experience team, and a yearly review that repeats the defect classification. If the share of escaped data access defects falls from 35 to 10 percent, the standard worked, and the review can shift attention to the next largest group, perhaps integration between services and contract tests.
This is the real answer to the two camps: we do not pick a winner once. We pick a method for deciding, write down today's result with examples, and re-run the method as the systems and tools change.
- Classify a year of escaped defectswhere, and which test would catch it
- Compare with current test mixfind the gaps
- Update the placement standarddefaults, examples, guardrails
- Teams apply, with explained variationnotes in each repository
- Measure escapes and suite timefeed the next review
5 steps in order. Play them, or step through them one at a time.
7Follow-up questions
Doesn't Testcontainers make CI depend on Docker?
Yes, so the standard includes the infrastructure: CI runners with a container runtime, an internal registry mirror for approved images, and a budget for that capacity. Without it, teams quietly fall back to mocks.
What if a team has no defect data yet?
Then they start with the default placement rules and begin classifying defects now. Within a few months they have enough evidence to justify any local variation.
How do you keep container tests fast?
Share containers across the run with a singleton pattern or Spring's context cache, isolate tests with unique data rather than restarting containers, and keep the number of distinct Spring contexts small.
8Conclusion
In this answer, we saw that the mocking camp and the container camp optimise for different defects, so the decision should come from our own defect data. Cheap containers now make real databases and brokers the default for our own infrastructure, while mocks stay right for slow, costly or uncontrollable third-party boundaries. We wrote down a short placement standard with examples and guardrails, allowed teams to vary with evidence, and gave it an owner and a yearly review so the direction stays valid as tools change.
Open with
I would not settle it as a matter of taste. I would take a year of escaped defects, classify each by where the bug was and which cheapest test would have caught it, and let that decide.
Then, if they want more
In most Spring estates a large share are SQL, mapping, migration and integration bugs, which mocked tests cannot see by design, while pure logic bugs are well served by unit tests.
The other big change is cost: Testcontainers with Spring Boot's ServiceConnection gives a real PostgreSQL or Kafka in seconds, so for infrastructure we own, real instances are now cheaper than maintaining mocks that only approximate them. Mocks remain right for slow, costly or uncontrollable boundaries such as payment providers and partner APIs, behind our own adapters.
What I would write down is a short standard: the principle, a placement table saying which test type to use for logic, services, repositories, messaging and third-party adapters, with real examples from our code, and a few guardrails like a CI time budget. Teams may vary when their own data supports it.
The standard has an owner and a yearly review that repeats the defect classification, so it changes when the tools and our systems do.
About this answer
- Last updated
- 12 November 2026
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- JUnit user guide, Mockito documentation
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "You are setting a ten-year test architecture direction. Some teams argue for...": https://scrivio-interview-library.netlify.app/java/mocking/9bc9254b37/