Quick revision
The short answer
I would lead it like any reliability problem: first measure rerun rates, flaky tests by owner and CI time lost, and make the numbers visible; then set a policy that a flaky test is quarantined within a day, has an owner, and is fixed or deleted within a fixed time. The lasting gains come from fixing root causes such as fixed sleeps, tight timeouts, shared state, external dependencies, time and randomness, and from investing in deterministic test infrastructure, not from automatic retries, which hide real concurrency bugs.
What to remember
In this answer, we treated flakiness as a measurable cost: we detected flaky tests from CI results, published the numbers by team, and set a policy of quarantine within a day, a named owner and a fixed deadline. We went through the common root causes of sleeps and tight timeouts, shared state, external dependencies, time and randomness, backed the fixes with deterministic infrastructure such as an injected Clock and containers, refused automatic retries as the main fix because they hide real concurrency bugs, and made the numbers part of each team's regular review so the gains last.
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 lead it like any reliability problem: first measure rerun rates, flaky tests by owner and CI time lost, and make the numbers visible; then set a policy that a flaky test is quarantined within a day, has an owner, and is fixed or deleted within a fixed time. The lasting gains come from fixing root causes such as fixed sleeps, tight timeouts, shared state, external dependencies, time and randomness, and from investing in deterministic test infrastructure, not from automatic retries, which hide real concurrency bugs.
Why this question is asked
A flaky test is one that passes and fails on the same code without any change. One flaky test is a nuisance; a few hundred of them across an organisation become a tax that every engineer pays several times a day, in reruns, in waiting, and in the slow loss of trust that makes people ignore red builds. Interviewers ask this leadership question to see whether we can turn a vague complaint into a programme with numbers, policy, engineering work and incentives that hold up over time. In this answer, we will define the problem precisely, measure it, set the policy, go through the root causes with code, describe the infrastructure that prevents them, explain why retries are not the answer, and finish with how we would keep the gains.
1A Quick Recap: What Makes a Test Flaky
A test is flaky when its result depends on something other than the code it tests: timing, the order in which tests run, the state of a shared environment, the current time, or a random value. In TestNG and JUnit suites, timeouts are the most visible symptom. A test with timeOut = 2000 passes in 1.4 seconds on a quiet laptop and fails in 2.3 seconds on a busy CI agent, so the same commit goes red and green.
The cost adds up fast. Suppose 400 engineers each hit a flaky failure twice a week and each rerun costs 20 minutes of waiting and context switching. That is 400 x 2 x 20 minutes, about 267 hours a week, or the work of six or seven full-time engineers, before counting the real bugs that get merged because people assumed a red build was just flaky again.
2Measure and Make It Visible
We cannot manage what we cannot see, so the first step is data. The CI system already has what we need: every test result for every commit. A test that both passed and failed on the same commit is flaky by definition, with no human judgement needed. From that we compute the rerun rate per pipeline, the list of flaky tests with their failure frequency, the owning team for each, and the CI minutes and developer hours lost.
Then we publish it. A weekly dashboard with the top 20 flaky tests, grouped by team, turns an invisible annoyance into a ranked list of work. Often the first publication alone fixes part of the problem, because many teams did not know which of their tests were the worst.
- Detect: pass and fail on the same commitautomatic, from CI results
- Quarantine within one working daystill runs, no longer blocks merges
- Assign to the owning teamticket with failure history
- Fixed within 10 working days?YesReturn to the blocking suiteNoEscalate, then delete or rewrite
4 steps in order. Play them, or step through them one at a time.
3A Policy With Short Deadlines
The policy has three parts. First, a flaky test is quarantined within one working day: it is moved to a non-blocking group, so it still runs and still reports, but it no longer blocks anyone's merge. Second, it is owned: the team that owns the code gets a ticket with the failure history attached. Third, it has a deadline, for example ten working days, after which it is either fixed and returned to the blocking suite or deleted and replaced with a better test.
The deadline is what makes quarantine honest. Without it, the quarantine group grows into a second suite nobody looks at, and coverage quietly disappears.
4Fix the Root Causes
Most flaky tests fall into a few families. Fixed sleeps and tight timeouts are the first. A test that sleeps for one second and hopes the message has arrived is wrong on a fast machine and on a slow one. The fix is to wait for the condition, with a generous upper limit.
Notice that the fixed version is usually faster as well as more reliable, because it stops waiting as soon as the condition holds. The same reasoning applies to TestNG's timeOut: a limit of 2000 milliseconds on an operation that normally takes 1500 is a coin toss on shared hardware, so timeouts should be generous guards against hangs, not performance assertions.
The other families are shared state, such as static fields, fixed database ids or a shared test account that two tests modify at once; external dependencies, such as a partner sandbox or a shared staging service that is down or slow; and time and randomness, such as a test that fails at midnight or on the last day of the month, or one that uses an unseeded random generator. Each has a known fix, published in a short guide.
5Invest in Deterministic Test Infrastructure
Root-cause fixes stay fixed only if the right thing is also the easy thing. So part of the programme is platform work. We inject a java.time.Clock into code that needs the current time, so tests can use a fixed or controllable clock. We provide Testcontainers setups so every test run gets its own fresh database or Kafka broker instead of a shared one. We replace calls to shared staging services with contract tests and local stubs, so a partner's outage does not fail our build.
This work is what moves the numbers permanently, because a team with a working container setup and a controllable clock rarely writes the next flaky test.
6Why Automatic Retries Are Not the Fix
The tempting shortcut is to rerun failures automatically, with TestNG's IRetryAnalyzer or Maven Surefire's rerunFailingTestsCount. It makes the dashboard green overnight, which is exactly the problem. A test that fails one time in twenty because of a real race condition in our code, for example two threads updating the same cache entry, is telling us about a production bug. A retry turns that signal into a pass.
Retries also hide cost: a suite that needs three attempts for 5 percent of its tests is slower, and nobody sees why. So the policy allows retries only as a recorded, temporary measure for a quarantined test, and every retry that turned a failure into a pass is reported as a flaky result, not as a pass.
7Keeping the Gains
Programmes like this often show big early wins, such as the rerun rate dropping from 15 to 3 percent in a quarter, and then slide back. To keep it, we make the flaky-test numbers part of each team's regular engineering health review, we keep the dashboard public, we check new tests for sleeps and fixed ids in code review, and we celebrate deletions of bad tests as much as additions of good ones. The goal we state to leadership is concrete: for example, fewer than 1 percent of pipeline runs needing a rerun, and no test in quarantine longer than ten working days.
8Follow-up questions
How do you get teams to spend time on flaky tests instead of features?
By showing the cost in their own terms. A team's dashboard row that says your flaky tests cost 30 hours of other people's time last month is persuasive. We also agree with engineering leadership that quarantine deadlines are part of the definition of done, so the work is planned rather than squeezed in.
How do you detect flaky tests that have not failed yet?
We can run suites several times on the same commit at night, in random order and with higher parallelism, which shakes out order dependencies and races before they hit developers. JUnit Jupiter can randomise method order, and TestNG suites can be run with different parallel settings to expose shared state.
9Conclusion
In this answer, we treated flakiness as a measurable cost: we detected flaky tests from CI results, published the numbers by team, and set a policy of quarantine within a day, a named owner and a fixed deadline. We went through the common root causes of sleeps and tight timeouts, shared state, external dependencies, time and randomness, backed the fixes with deterministic infrastructure such as an injected Clock and containers, refused automatic retries as the main fix because they hide real concurrency bugs, and made the numbers part of each team's regular review so the gains last.
Open with
I would start with measurement, because flakiness is usually felt but not counted. CI already has the data: any test that passes and fails on the same commit is flaky.
Then, if they want more
From that I would publish the rerun rate, the top flaky tests by owning team, and the hours lost, which in a 400-engineer organisation can easily be the equivalent of several full-time people.
Then I would set a simple policy: a flaky test is quarantined within one working day so it stops blocking merges but keeps running, it gets an owner and a ticket, and within about ten working days it is either fixed and returned or deleted. The deadline is what stops quarantine from becoming a graveyard.
The real work is root causes. Fixed sleeps and tight timeOut values become waits on a condition with a generous limit, shared state like static fields or shared accounts is removed, external dependencies are replaced with containers or stubs, and time and randomness are controlled through an injected Clock and seeded random values.
I would fund a platform team to make that the easy path, with Testcontainers setups and contract tests instead of shared environments. I would not use automatic retries as the fix, because a test failing one time in twenty may be revealing a real race condition, and a retry hides it.
Finally, I would keep the numbers in each team's health review so the improvement lasts.
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 "Flaky timeouts in TestNG and JUnit suites cost your engineering organisation many hours...": https://scrivio-interview-library.netlify.app/java/testng/9959add8d9/