Quick revision
The short answer
I would define test layers with a clear job each: many unit tests for logic, slice tests for the web and persistence layers, and a small number of @SpringBootTest integration tests against real dependencies through Testcontainers. Between services, contract tests replace broad end-to-end environments. The standard comes with shared test starters and templates in the golden path, build-time and flakiness budgets tracked per service, and is spread through examples and pairing rather than a document alone.
What to remember
In this answer, we saw why suites made only of @SpringBootTest tests or only of mocks both fail, and defined a layered standard: unit tests for logic, slice tests for web and persistence on real engines, a small set of @SpringBootTest tests with Testcontainers, and contract tests between services instead of a shared end-to-end environment. We made it stick with a shared test starter and templates, build-time and flakiness budgets per service, and adoption through pilots, examples and pairing.
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 define test layers with a clear job each: many unit tests for logic, slice tests for the web and persistence layers, and a small number of @SpringBootTest integration tests against real dependencies through Testcontainers. Between services, contract tests replace broad end-to-end environments. The standard comes with shared test starters and templates in the golden path, build-time and flakiness budgets tracked per service, and is spread through examples and pairing rather than a document alone.
Why this question is asked
When 50 services each test in their own way, the organisation pays twice. Teams with only @SpringBootTest tests have slow, fragile builds; teams with only mocks have fast builds that pass while production breaks, because nothing ever checked the real database or the real JSON. Moving engineers between teams is harder, and shared tooling cannot help anyone. Interviewers ask this to see whether an architect can set a standard that is specific enough to act on and light enough that teams adopt it. In this answer, we will look at why both extremes fail, define the layers, deal with testing between services, and then cover how to make the standard stick.
1Why Both Extremes Fail
A suite made only of @SpringBootTest tests checks a lot per test, but each test starts the whole application, often 5 to 15 seconds when contexts are not shared. Edge cases of business logic are tested through HTTP calls, which is slow and makes a failure hard to locate. Such builds drift toward 20 or 30 minutes, and teams start skipping tests.
A suite made only of mocks has the opposite problem. Every repository and client is a Mockito mock, so tests run in seconds, but nothing checks that the SQL is valid on PostgreSQL, that the JSON field is called orderId and not order_id, or that the security configuration actually protects an endpoint. Those are exactly the defects that escape. So the standard needs both kinds of test, each in the place where it is strong.
2Defining the Layers
I would describe the standard as layers, each with a purpose and a rough share of the suite, so a team can look at a new test and know where it belongs.
Unit tests cover business logic, such as pricing, validation and state transitions, with plain JUnit and no Spring context. They should be the majority and run in milliseconds. Slice tests cover one technical layer with a small context: @WebMvcTest for controllers, request mapping, validation and security rules; @DataJpaTest for queries and mappings, run against the real database engine in a container rather than an in-memory one. A small number of @SpringBootTest integration tests, perhaps ten to thirty per service, prove that the whole application starts and that key flows work end to end inside the service, with real infrastructure from Testcontainers.
- Critical journey checksa handful across the platform, after deploy
- Contract testsbetween services, consumer and provider
- @SpringBootTest integration10 to 30 per service, real infrastructure
- Slice tests
@WebMvcTest,@DataJpaTeston a real engine - Unit testsmost tests, logic only, no Spring
5 layers, from the top down. Step through them one at a time.
Lower layers are faster and more numerous; each higher layer checks what the lower ones cannot.
3Testing Between Services With Contracts
With 50 services, the tempting way to test integration is a shared environment where everything is deployed together. In practice such environments are slow, often broken by someone else's deploy, and block releases. Instead, the standard uses contract tests: the consumer of an API writes down the requests it sends and the responses it relies on, and the provider's build verifies that it still satisfies those expectations. Tools such as Pact or Spring Cloud Contract automate this.
The reason contracts work better is that each check runs in one team's build, against one service, with a clear owner when it fails. A broken contract is found before the provider deploys, not days later in a shared environment.
4Making the Right Way the Easy Way
A standard that requires each team to set up Testcontainers, base classes and contract tooling from scratch will be adopted by the enthusiastic teams only. So the platform provides it in the golden path, the supported way to build a service: a shared test starter dependency with container definitions and pinned images, base test classes per layer, and a service template where each layer already has one working example test.
Notice what the team does not have to decide: the image version, the container setup, or how to wire the connection. The starter also gives every service the same structure, which makes moving between teams easier.
5Budgets, Measurement and Adoption
To know whether the standard works, I would set two budgets and track them per service on a shared dashboard: build time, for example under 10 minutes for the main pipeline, and flakiness, for example fewer than 1 percent of runs failing and then passing on retry. Alongside them, I would watch escaped defects by kind, because the purpose of the layers is to catch bugs earlier, not just to be faster.
Finally, adoption happens through people. I would pick two or three pilot teams, one from each extreme, help them move to the standard by pairing, and use their before-and-after numbers as the example for everyone else. Reviews of test code in those teams, and a short internal talk with real examples, teach far more than a written guideline. The written document still exists, but as the reference, not as the main teaching tool.
- Write the layers and examplesshort, with one test per layer
- Ship the test starter and templategolden path
- Pilot with two or three teamspairing, measure before and after
- Publish budgets and dashboardbuild time, flakiness, escaped defects
- Roll out to the restteams adopt when they next touch tests
5 steps in order. Play them, or step through them one at a time.
6Follow-up questions
Would you forbid @SpringBootTest in unit-level tests?
Not with a hard rule, but the guideline and review would push logic tests down to plain unit tests. A build-time budget does most of the work: a team that writes everything with @SpringBootTest will hit it and look for faster options.
Why use a real database in @DataJpaTest instead of H2?
Because the bugs we want to catch there are dialect bugs, such as a query using a PostgreSQL feature, a constraint, or a migration that only fails on the real engine. An in-memory database with different behaviour gives false confidence, and Testcontainers makes the real engine cheap enough.
How do you handle teams that disagree with the standard?
Ask for their data. If a team has fast builds and few escaped defects with a different mix, the standard should allow it. The layers describe goals; teams can deviate when they show the results are as good.
7Conclusion
In this answer, we saw why suites made only of @SpringBootTest tests or only of mocks both fail, and defined a layered standard: unit tests for logic, slice tests for web and persistence on real engines, a small set of @SpringBootTest tests with Testcontainers, and contract tests between services instead of a shared end-to-end environment. We made it stick with a shared test starter and templates, build-time and flakiness budgets per service, and adoption through pilots, examples and pairing.
Open with
I would define the standard as layers, each with one job. Most tests are plain unit tests for business logic, with no Spring context. Then slice tests: @WebMvcTest for controllers, validation and security rules, and @DataJpaTest for queries, run against the real database engine with Testcontainers rather than H2.
Then, if they want more
Then a small number of @SpringBootTest integration tests per service, maybe ten to thirty, with real infrastructure, proving that key flows work inside the service. Between services, I would use contract tests, like Pact or Spring Cloud Contract, instead of a shared end-to-end environment, because a broken contract then fails in one team's build with a clear owner.
To get 50 teams to adopt it, I would put it in the golden path: a shared test starter with container definitions and pinned images, base classes per layer, and a service template that already has one example of each kind of test.
I would set budgets, for example a main build under ten minutes and flakiness under one percent, and track them per service with escaped defects. And I would roll it out through two or three pilot teams with pairing, then use their numbers as the example, because people adopt practices from working code much more than from a document.
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
- Spring Boot reference, Testing
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 "Across 50 Spring Boot services, test practices vary widely: some teams only have...": https://scrivio-interview-library.netlify.app/spring-boot/spring-boot-test/97753ca5cb/