Quick revision
The short answer
I would define a small, fixed tag vocabulary (unit, integration, contract, e2e) with a runtime budget for each, enforced by a build check, and run the tiers in different places: unit tests on every commit, integration and contract tests in parallel stages, end-to-end tests on merge or before release. Test impact analysis picks the affected modules for each change, a full nightly run is the safety net, and parallel execution plus sharding, together with owners for slow and flaky tests, keep the times down.
What to remember
In this answer, we started from measured targets, defined a small tag vocabulary with meanings and budgets enforced by ArchUnit, and placed each tier where its feedback is worth its cost: unit tests on every commit, integration and contract tests in parallel stages, end-to-end tests on merge. Module-level test impact analysis selects what each change needs, with a nightly full run as the safety net, and parallel execution, sharding, context reuse and moving tests down the pyramid make the remaining work faster. Owners for slow and flaky tests keep it that way.
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 a small, fixed tag vocabulary (unit, integration, contract, e2e) with a runtime budget for each, enforced by a build check, and run the tiers in different places: unit tests on every commit, integration and contract tests in parallel stages, end-to-end tests on merge or before release. Test impact analysis picks the affected modules for each change, a full nightly run is the safety net, and parallel execution plus sharding, together with owners for slow and flaky tests, keep the times down.
Why this question is asked
A 50-minute pipeline changes how people work. Developers batch changes to avoid waiting, merges pile up, and a failure found after an hour costs a context switch. Interviewers ask this design question at senior level because the answer combines testing knowledge, build engineering and team habits, and because there is no single trick that fixes it. In this answer, we will set targets, define the tag vocabulary and enforce it, decide which tiers run where, add test impact analysis with a safety net, speed up what remains with parallelism and sharding, and keep the system healthy with ownership of slow and flaky tests.
1Setting the Targets First
Before changing anything, we measure where the 50 minutes go. A typical breakdown in a Java monorepo is something like 25,000 unit tests taking 12 minutes, 4,000 integration tests with databases and Spring contexts taking 25 minutes, 800 contract tests taking 5 minutes and 200 end-to-end tests taking 8 minutes, all run one after another on every commit.
Then we set targets that people feel: for example, feedback on a pull request in under 10 minutes for a typical change, the full verification before merge in under 20, and nothing that ever blocks a developer for 50. The targets turn a vague wish into decisions about what runs where.
2A Small Tag Vocabulary With Budgets
Tags only help if everyone uses the same few names with the same meaning. So we fix a vocabulary of four and give each a definition and a time budget per test. A unit test has no Spring context, no network and no database, and runs in under 50 milliseconds; it carries no tag. An integration test starts a Spring slice or a Testcontainers database and stays under about 2 seconds. A contract test checks an API or event schema against a consumer contract. An end-to-end test runs against deployed services.
We give each tag a composed annotation, @IntegrationTest, @ContractTest and @EndToEndTest, so the names cannot be misspelled, and we add a build check that enforces the definitions. An ArchUnit rule can say that any class using @SpringBootTest or a Testcontainers container must carry the integration tag, and a report job flags any untagged test that took longer than its budget.
| Tag | Meaning | Budget per test | Runs |
|---|---|---|---|
| (none) unit | no context, no I/O | under 50 ms | every commit, locally |
| integration | Spring slice or container | under 2 s | pull request, parallel stage |
| contract | API or event schema vs consumers | under 1 s | pull request, parallel stage |
| e2e | against deployed services | minutes | on merge, before release |
Notice that the rule checks the structure of the code, not the runtime. That keeps the tiers honest: a slow test cannot hide in the unit tier by forgetting a tag.
3Running Each Tier in the Right Place
Each tier answers a different question, so it belongs at a different point in the pipeline. Unit tests answer 'did I break the logic' and must be fast enough to run locally and on every push. Integration and contract tests answer 'does this still work with real infrastructure and with our consumers'; they run on the pull request, but in separate stages that start at the same time, so their times do not add up. End-to-end tests answer 'does the whole system still work together'; they are slow and fragile, so they run after merge on the main branch, and before a release.
This alone usually cuts the pull request time sharply, because the slowest tier leaves the critical path and the other tiers run side by side instead of one after another.
- Push to a pull request
- Impact analysis picks affected modules
- Unit tests of affected modulesabout 3 minutes
- Integration and contract stages in parallelsharded, about 8 minutes
- All green?YesMerge to mainNoFix and push again
- On main: end-to-end testsbefore deployment
- Nightly: every test in every modulethe safety net
7 steps in order. Play them, or step through them one at a time.
4Selecting Tests With Impact Analysis
Most changes touch one or two of the hundreds of modules in a monorepo, so running all 30,000 tests for each change wastes most of the time; more machines only divide that waste. Test impact analysis selects the tests that could be affected. The simplest reliable version works at module level: from the changed files, find the changed modules, then add every module that depends on them through the build graph, and run the tests of that set. Gradle and Bazel know this graph; for Maven, the -pl and -amd options build a module and everything that depends on it.
Finer selection is possible, using coverage data to map each test to the classes it touches, or a commercial predictive selection service, but module-level selection is easy to trust and explain. Its weakness is changes that the graph cannot see, such as a shared configuration file, a database migration or a change to the build itself. So we treat those paths as 'run everything' triggers, and we keep a full run nightly, and before each release, as the safety net. If the nightly run finds a failure that the pull request selection missed, that is a bug in the selection rules and we fix the rules.
5Making the Rest Faster
What remains can still be sped up. JUnit 5 supports parallel execution inside one JVM: setting junit.jupiter.execution.parallel.enabled=true and a default mode of concurrent in junit-platform.properties runs test classes or methods on several threads. It works well for unit tests and needs care with shared state, so we enable it per module once its tests are known to be independent, and use @ResourceLock or @Isolated for the exceptions.
Across machines we shard: the integration stage runs on, say, 8 agents, and each agent takes a slice of the test classes, balanced by the durations recorded in earlier runs. Gradle's maxParallelForks and Surefire's forkCount add parallel JVMs on one agent. For Spring integration tests, the biggest win is often fewer application contexts: tests that share one configuration reuse a cached context, while each distinct mix of mocked beans starts a new one.
The last lever is the test pyramid itself. When an end-to-end test checks something that a unit or contract test could check, we move it down a tier. That makes it faster and less flaky at the same time.
6Owning Slow and Flaky Tests
Pipelines get slow again unless someone watches. We collect durations and results from the JUnit XML reports of every build, show the slowest tests and the flakiest tests per team, and assign each one to its owning team with a ticket. A flaky test, one that fails and passes on the same commit, is quarantined into a separate non-blocking job with a deadline, so it stops blocking everyone while it is fixed. Each tier's budget is reviewed regularly, and a test that exceeds its budget is either made faster or moved to the right tier.
7Follow-up questions
Is it safe to skip tests on a pull request?
It is safe enough when the selection is conservative and there is a safety net. Module-level impact analysis rarely misses a real dependency, triggers for global changes force a full run, and the nightly full run catches the rest within a day. The risk is a broken main branch for a few hours, which is far cheaper than every developer waiting 50 minutes.
How do you decide whether a test is unit or integration?
By what it touches, not by how long it takes today. A test that starts a Spring context, a container, a file system or the network is an integration test, and the ArchUnit rule enforces that. The time budget then tells us when a test in a tier is unusually slow.
8Conclusion
In this answer, we started from measured targets, defined a small tag vocabulary with meanings and budgets enforced by ArchUnit, and placed each tier where its feedback is worth its cost: unit tests on every commit, integration and contract tests in parallel stages, end-to-end tests on merge. Module-level test impact analysis selects what each change needs, with a nightly full run as the safety net, and parallel execution, sharding, context reuse and moving tests down the pyramid make the remaining work faster. Owners for slow and flaky tests keep it that way.
Open with
I would start by measuring where the 50 minutes go and setting a target, say under 10 minutes for a typical pull request. Then I would define a small tag vocabulary: untagged unit tests with no context or I/O, integration for anything with a Spring context or container, contract for API and event contracts, and e2e for deployed systems, each with a time budget.
Then, if they want more
Composed annotations make the tags consistent, and an ArchUnit rule fails the build if, for example, a @SpringBootTest class is not tagged integration. Next, each tier runs where it pays off: unit tests on every push, integration and contract tests in parallel stages on the pull request, end-to-end tests after merge and before release.
To avoid running 30,000 tests for a one-module change, I would use test impact analysis from the build graph, the changed modules plus everything that depends on them, with global changes like build files or migrations triggering a full run, and a full nightly run as the safety net.
Then I would speed up the rest with JUnit parallel execution where tests are independent, sharding across agents by recorded durations, fewer distinct Spring contexts, and moving slow tests down the pyramid. Finally, slow and flaky tests get owners, flaky ones are quarantined with a deadline, and the dashboards are reviewed regularly.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- JUnit 5
- 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
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 "Your monorepo has 30,000 tests and a 50-minute pipeline. Design a tagging and...": https://scrivio-interview-library.netlify.app/java/junit/dd7a49bb37/