Quick revision
The short answer
I would measure first, per service: contexts created per build from the TestContext cache statistics, container start time, and the slowest test classes. Then I would publish shared test base configurations so tests reuse cached contexts, give teams platform test support with shared Testcontainers definitions, @ServiceConnection, pinned images and pre-pulled CI images, move logic tests to unit tests or slices and remove unneeded @DirtiesContext, and set a build-time budget per service with trends on a dashboard so regressions are noticed.
What to remember
In this answer, we measured contexts, container start time and slow classes per service, then cut the cost at the platform level: shared base configurations so contexts are reused, a test library with pinned container definitions, @ServiceConnection and pre-pulled CI images, logic tests moved to unit tests or slices with unneeded @DirtiesContext removed, and per-service build-time budgets shown as trends on a dashboard so regressions are caught early.
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 measure first, per service: contexts created per build from the TestContext cache statistics, container start time, and the slowest test classes. Then I would publish shared test base configurations so tests reuse cached contexts, give teams platform test support with shared Testcontainers definitions, @ServiceConnection, pinned images and pre-pulled CI images, move logic tests to unit tests or slices and remove unneeded @DirtiesContext, and set a build-time budget per service with trends on a dashboard so regressions are noticed.
Why this question is asked
When 40 services each create 20 to 60 application contexts and start their own containers on every build, the waste is multiplied many times: thousands of startups a day across the organisation, each one buying no extra confidence. An architect cannot fix 40 test suites by hand, but can change the defaults every team builds on. Interviewers ask this to see whether we can find the shared cause, design platform-level fixes, and keep the improvement from fading. In this answer, we will set up the measurements, explain where the time goes, and then cover the shared configurations, the platform container support, test placement, and budgets.
1Measuring Before Changing Anything
I would start with a small script or CI plugin that every service can adopt in one line, collecting three things per build. The number of contexts created comes from the Spring TestContext cache statistics, logged when org.springframework.test.context.cache is at DEBUG level: the miss count is the number of context startups. Container start time comes from the Testcontainers logs or a simple timer around startup. And the slowest test classes come from the build tool's test reports.
With that data from all 40 services in one place, the picture usually becomes clear within a week. For example: a median of 35 contexts per build at about 7 seconds each, so roughly 4 minutes of startups; containers started per test class rather than per JVM, adding 2 to 3 minutes; and a handful of services with many @DirtiesContext uses.
| Measure | Before | Target |
|---|---|---|
| Contexts created per build | 48 | under 8 |
| Time spent starting contexts | 5.6 min | under 1 min |
| Containers started per build | 60 (per class) | 3 (per JVM) |
@DirtiesContext uses | 22 | only where justified |
| Main build time | 21 min | under 10 min |
2Shared Base Configurations So Contexts Are Reused
The largest saving comes from context reuse. Contexts multiply because each test class varies a little: different mocks, profiles or properties. The platform's test library would publish a small set of base configurations, for example one for API tests and one for messaging tests, each with a fixed set of profiles, properties, containers and mocks. Teams extend them instead of composing their own.
Notice that the team writes nothing about containers or profiles. The fewer choices each test class makes, the more classes share a key, and in a typical service the context count drops from dozens to a few.
3Platform Support for Containers
Containers are the second cost. The test library defines the standard containers once, with image versions pinned to what the platform runs in production, and exposes them with @ServiceConnection so Spring Boot wires the connection details automatically. Declared as beans in a shared configuration, or as static fields, they start once per JVM and are reused by every context, rather than once per test class.
On the CI side, the platform team pre-pulls those pinned images onto runners, or keeps them in a local registry mirror, so no build waits to download a 400 MB image. Because the versions are pinned in one place, upgrading PostgreSQL for all 40 services is a single change in the library, tested once.
- Service testsextend a platform base class
- Platform test librarybase configurations, container beans with
@ServiceConnection - Pinned imagesversions matching production
- CI runnersDocker available, images pre-pulled or mirrored
4 layers, from the top down. Step through them one at a time.
4Putting Each Test at the Right Layer
Some of the integration tests do not need integration at all. Business rules tested through a full context become plain unit tests; controller tests become @WebMvcTest; repository tests become @DataJpaTest against the shared container. And @DirtiesContext, usually added once to work around shared state, is removed wherever the state can be reset directly in the test.
This is where confidence is protected rather than reduced. The integration tests that remain test integration, wiring, persistence, messaging, and run against real engines, while logic gets more thorough and faster unit tests.
5Budgets and a Dashboard So It Stays Fixed
Improvements like this fade unless someone watches them. I would set a build-time budget per service, for example a main build under 10 minutes, and publish the collected measurements on a dashboard with trends: build time, contexts per build and container starts. A service whose context count jumps from 4 to 15 after a change shows up the next day, while the cause is fresh. The platform team reviews the outliers monthly and helps those teams, and the best before-and-after examples become the documentation for everyone else.
6Follow-up questions
Why not simply give CI bigger machines?
Bigger machines make each startup a little faster but do not remove the repeated work, and the cost is paid on every build of 40 services. Removing redundant startups saves far more and also shortens developers' local runs.
How do you get 40 teams to adopt the base configurations?
Make adoption a one-line change, migrate two pilot services with their teams, and publish the before-and-after build times. Once the dashboard shows peers at 8 minutes, most teams ask for help instead of needing to be told.
Does reusing containers between tests risk flaky tests?
Only if tests depend on leftover data. The standard should say that each test cleans up or uses unique data, for example unique ids or a table truncation before each test, which is good practice with any shared database.
7Conclusion
In this answer, we measured contexts, container start time and slow classes per service, then cut the cost at the platform level: shared base configurations so contexts are reused, a test library with pinned container definitions, @ServiceConnection and pre-pulled CI images, logic tests moved to unit tests or slices with unneeded @DirtiesContext removed, and per-service build-time budgets shown as trends on a dashboard so regressions are caught early.
Open with
I would start by measuring, the same way across all 40 services. I would add a small CI step that captures the TestContext cache statistics, so we know how many contexts each build creates, plus container start times and the slowest test classes.
Then, if they want more
Usually that shows dozens of context startups per build, each several seconds, and containers started per test class. The biggest fix is context reuse: the platform test library would publish a few base configurations, such as one for API tests and one for messaging, with fixed profiles, properties, mocks and containers, so most test classes share the same cache key.
Then platform container support: shared Testcontainers definitions with @ServiceConnection, image versions pinned to what production runs, containers started once per JVM, and images pre-pulled on CI runners.
Next, test placement: logic tests move to plain unit tests, web and persistence tests to slices, and unneeded @DirtiesContext gets removed, so the remaining integration tests are really about integration and confidence stays the same.
Finally, I would set a build-time budget per service, say ten minutes, and show build time and contexts per build on a dashboard with trends, so a regression is noticed the next day, with the platform team helping the outliers.
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 40 Spring Boot services, CI time is dominated by integration tests: each build...": https://scrivio-interview-library.netlify.app/spring-boot/spring-boot-test/85e85b6274/