Quick revision
The short answer
I would combine changes at the JUnit level and at the suite level. At the JUnit level, make tests isolated so parallel execution can be enabled safely. At the suite level, split slow suites across agents using historical timing, run only the tests affected by a change on pull requests with full runs on main, and remove duplicated and low-value tests found with coverage and mutation data. To prove that confidence did not drop, measure escaped defects and pipeline signals before and after.
What to remember
In this answer, we started by measuring where CI time and cost go, then combined JUnit-level isolation and parallel execution with fewer Spring contexts, timing-based sharding for waiting time, affected-test selection on pull requests with full runs on main, and removal of duplicated and low-value tests found through coverage and mutation data. To meet the requirement of no lost confidence, we measured escaped defects, main-branch failures and mutation scores before and after each 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 combine changes at the JUnit level and at the suite level. At the JUnit level, make tests isolated so parallel execution can be enabled safely. At the suite level, split slow suites across agents using historical timing, run only the tests affected by a change on pull requests with full runs on main, and remove duplicated and low-value tests found with coverage and mutation data. To prove that confidence did not drop, measure escaped defects and pipeline signals before and after.
Why this question is asked
When CI time becomes a major cloud cost, the request to cut it by 40 percent usually comes with an unspoken fear: that we will cut it by testing less and ship more bugs. A good answer reduces machine time and waiting time while keeping, and measuring, the protection the tests give. Interviewers ask this scenario to see whether we can plan a cost program with numbers, not just list tricks. In this answer, we will start by measuring where the time goes, then work through isolation and parallel execution, timing-based splitting, running affected tests, removing low-value tests, and finally how to show confidence held.
1Measuring Where the Time Goes
Before choosing strategies we need a baseline. From the test reports of the last few weeks we collect, per module and per test class, the duration, the failure rate and how often each runs. Usually the picture is uneven: perhaps 10 percent of classes, mostly @SpringBootTest classes with containers, take 60 percent of the time, and some modules rebuild and rerun everything on every pull request even when untouched. We also split cost into compute minutes, which drive the cloud bill, and wall-clock time, which drives developer waiting; some measures help one more than the other.
Say the baseline is 600,000 agent-minutes a month, with pull requests making up 70 percent of it. The target is then 360,000, and we can estimate each strategy's share.
| Strategy | Main effect | Estimated saving |
|---|---|---|
| Isolation + JUnit parallel execution | fewer agent-minutes per run | 15 percent |
| Affected tests only on pull requests | fewer tests per PR run | 20 percent |
| Remove duplicated and low-value tests | smaller suite everywhere | 8 percent |
| Fewer Spring contexts per suite | less startup per run | 5 percent |
| Timing-based splitting | shorter wall-clock time | small on cost, big on waiting |
2Making Tests Isolated So Parallel Execution Is Safe
JUnit can run test classes concurrently on one agent with junit.jupiter.execution.parallel.enabled=true and concurrent class mode. On a 4-core agent that is mostly idle while one test runs, this can cut a module's time sharply. But it only works if tests are isolated: no shared static state, no rows left in shared tables, no changes to global time zone or system properties. So the first program of work is isolation, finding and fixing tests that interfere, and declaring @ResourceLock where tests truly share a resource.
A related saving is Spring context reuse. Each distinct combination of test configuration, profiles, @MockitoBean declarations and properties creates another application context, and each one may cost several seconds and a container. Standardising test configurations so that more classes share a cached context, and removing unnecessary @DirtiesContext, often saves minutes per module.
3Splitting Slow Suites Across Agents
When one module's suite takes 30 minutes, the pull request waits 30 minutes even if everything else is done. Splitting the suite across several agents in parallel, sharding, cuts wall-clock time. The key is to split by historical timing, not by number of classes, so that each shard takes about the same time; otherwise one shard with all the slow integration tests decides the total. Most CI systems and build tools can store test timings and balance shards from them.
Notice that sharding mainly reduces waiting, not compute cost, and each shard pays its own startup. It complements the cost savings rather than providing them.
4Running Only Affected Tests on Pull Requests
The largest saving usually comes from not running tests that cannot be affected by a change. Build tools with good dependency graphs, such as Gradle with build caching or Maven with incremental build extensions, skip modules whose inputs did not change. Test impact analysis goes further, selecting the tests whose covered code was changed. On pull requests we run only affected modules and tests; on the main branch, after merge, we run the full suite, so anything the selection missed is caught within hours rather than at release.
- Change pushed
- Is it a pull request?YesRun affected modules and tests onlyNoRun the full suite on main
- Full suite on main finds a failure the PR missed?YesFix fast; tune the selection rulesNoSelection was safe
- Record timings and resultsfeeds sharding and test impact data
4 steps in order. Play them, or step through them one at a time.
5Removing Duplicated and Low-Value Tests
Large suites accumulate tests that add time but little protection: the same rule tested at unit, integration and end-to-end level; integration tests that only check what a unit test already checks; tests for code that no longer matters. Coverage data shows tests that cover exactly the same lines as others. Mutation testing, with a tool such as PIT, which makes small changes to the code and checks whether any test fails, shows which tests actually catch bugs: a slow test that kills no mutants that cheaper tests do not also kill is a candidate for removal or for replacement by a unit test.
Removal should be reviewed by the owning team, never done by script alone, and we keep at least one test at the right level for each important rule.
6Showing That Confidence Did Not Drop
Leadership asked for no loss of confidence, so we measure it. Before changes, we record a baseline of escaped defects, bugs found in production or in staging that tests should have caught, plus failures on main that pull requests missed, and the mutation score of key modules. After each step we track the same numbers. If escaped defects or main-branch failures rise, we tune the selection or restore tests.
A monthly report then shows both sides: agent-minutes down from 600,000 to, say, 350,000, and escaped defects and mutation scores flat. That is a far stronger answer than a promise.
7Follow-up questions
Isn't running only affected tests risky?
It is a trade-off, which is why the full suite still runs on main after every merge and nightly. The risk is that a change breaks something the selection did not consider, such as a configuration file; we limit it by treating shared configuration and build files as affecting everything, and we watch how often main catches what pull requests missed.
Which strategy would you start with?
With the measurement, then isolation and parallel execution on the slowest modules, because it benefits every later strategy and gives quick visible gains. Affected-test selection comes next, as it usually gives the largest cost cut, and test removal runs in parallel as a slower, reviewed effort.
How do you keep the savings from eroding?
By keeping the timing dashboard visible, setting a budget per module with an alert when it grows, and reviewing new slow test classes, for example any new class over 60 seconds, in code review.
8Conclusion
In this answer, we started by measuring where CI time and cost go, then combined JUnit-level isolation and parallel execution with fewer Spring contexts, timing-based sharding for waiting time, affected-test selection on pull requests with full runs on main, and removal of duplicated and low-value tests found through coverage and mutation data. To meet the requirement of no lost confidence, we measured escaped defects, main-branch failures and mutation scores before and after each change.
Open with
I would start by measuring, not cutting. From the last weeks of test reports I would find where the agent-minutes go, which usually shows a small set of Spring Boot integration classes and untouched modules rerunning on every pull request taking most of the time.
Then, if they want more
Then I would plan a few strategies with estimated savings. First, isolation: fix shared static state, leftover rows and global settings so JUnit's parallel execution can be turned on safely, and reduce the number of distinct Spring contexts so more classes reuse the cache.
Second, on pull requests run only the affected modules and tests using the build's dependency graph and test impact data, while the full suite runs on main after every merge and nightly, so anything missed is caught quickly. Third, split slow suites across agents using historical timing so shards finish together; that mainly cuts waiting time.
Fourth, remove duplicated and low-value tests using coverage and mutation testing, reviewed by the owning teams. And because leadership asked not to reduce confidence, I would baseline escaped defects, failures on main that pull requests missed, and mutation scores, and report them every month next to the cost, so we can show the 40 percent saving came without more bugs.
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
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 company's total CI test time is large enough to be a major cloud cost. Leadership...": https://scrivio-interview-library.netlify.app/java/junit/c9cda10855/