Scrivio Interview library
ScenarioIntermediate7 min read · updated 12 November 2026

Your team wants to add automatic retries for failing tests in CI (for example Surefire rerunFailingTestsCount or a retry extension). What are the risks and what would you do instead or in addition?

Automatic retries make the pipeline green but hide real intermittent bugs, such as races and time-zone problems, and they make slow builds slower. If we use them at all, every test that passed only on a retry must be reported as flaky and get a ticket automatically. Alongside that, known flaky tests go into a tagged quarantine job with an owner and a deadline, we fix the usual causes (shared state, uncontrolled time and randomness, ordering assumptions, external services), and we reproduce each failure locally with @RepeatedTest before fixing it.

Why this question is asked

Every team with a large test suite meets flaky tests, tests that sometimes pass and sometimes fail on the same code. Retrying them automatically is tempting because it is a one-line change: Surefire has rerunFailingTestsCount, Gradle has a test-retry plugin, and several JUnit extensions retry a test until it passes. Interviewers ask this because the honest answer is 'it depends on what you do with the information', and a good candidate can explain the risks and offer a better system. In this answer, we will look at what retries actually hide, how to use them safely if we must, how quarantine works, what usually causes flakiness, and how to reproduce a failure before fixing it.

1What a Retry Really Does

With Surefire, setting rerunFailingTestsCount to 2 means that a failing test is run up to two more times, and if any run passes, the build counts it as passed. Surefire reports such a test as a flake, and the XML report records the failed attempts, but the build itself is green. Gradle's test-retry plugin and JUnit Pioneer's @RetryingTest do similar things.

XML
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<rerunFailingTestsCount>2</rerunFailingTestsCount>
</configuration>
</plugin>
// Tests run: 1250, Failures: 0, Errors: 0, Skipped: 0, Flakes: 3
// BUILD SUCCESS

Notice the Flakes count. Three tests failed at least once and then passed. Unless something reads that number, nobody will ever look at those three tests again.

2The Risks: Hidden Bugs and Slower Pipelines

A flaky test is not always a bad test. Often it is a good test that found a bug which only shows up some of the time: a race between two threads, a cache that is read before it is filled, a date calculation that breaks when the test runs near midnight UTC or on the day the clocks change. Those bugs also happen in production, where there are no retries. A retry turns the one signal we had about them into silence.

Retries also cost time. A flaky integration test that takes 40 seconds and is retried twice adds 80 seconds whenever it fails, and with dozens of such tests the pipeline gets slower in exactly the way nobody notices. And because retries make flakiness cheap, the number of flaky tests tends to grow, until the suite is green only because of them.

What happens to a flaky test
ApproachBuildBug signalCost
No retriesred now and thenkept, but annoyingblocks merges
Silent retriesgreenlostslower, more flakes over time
Retries + flaky report + ticketgreenkept, ownedsmall, visible
Quarantine job + fixgreen, test still runskept, deadlinesmall

3If We Use Retries, Report Them

Retries can be acceptable as a short-term measure in a large, shared pipeline, as long as they never silence the signal. The rule is that a test which passed only after a retry is flaky by definition, because it both failed and passed on the same commit. We parse the XML reports after each build, find the tests with failed attempts, and record them in a store with the test id, the failure message and the commit.

From there a job opens a ticket for the owning team the first time a test flakes, and adds to it the next times. Gradle's test-retry plugin can also be configured to fail the build if a test passes on retry in certain modules, which is useful for modules that have been cleaned up and should stay clean.

4Quarantine With an Owner and a Deadline

Quarantine is the alternative that keeps the main pipeline reliable without hiding anything. A known flaky test gets a tag, for example @Tag("flaky"), and a reason with a ticket. The main pipeline excludes that tag, and a separate, non-blocking job runs only the flaky tests on every build, so we still collect their results.

Each quarantined test has an owner and a deadline, for example two weeks. If it is not fixed by then, the owning team decides to fix it, rewrite it or delete it. The quarantine list is visible, and its size is a number teams try to keep small.

Java
@Tag("flaky") // PAY-5120 fails near midnight UTC, owner: payments, due 2026-11-20
@Test
void dailyReportIncludesTodaysOrders() { ... }
// Main pipeline: -DexcludedGroups=flaky (blocking)
// Quarantine job: -Dgroups=flaky (non-blocking, results recorded)
Step throughLife of a flaky test
  1. Test fails, then passes on the same commit
  2. Recorded as flaky, ticket opened
  3. Tagged flaky, moved to quarantine job
  4. Owner reproduces it with @RepeatedTest
  5. Fixed within the deadline?
    YesRemove the tag, back in the main pipelineNoRewrite or delete with owner approval

5 steps in order. Play them, or step through them one at a time.

5Fixing the Common Causes

Most flaky tests come from a short list of causes, and each has a standard fix. Shared state comes first: static fields, a shared database without cleanup, or a cached Spring context modified by one test and read by another. The fix is isolation, fresh data per test, or @DirtiesContext as a last resort. Time and randomness come next: code calling LocalDate.now() or new Random() directly behaves differently depending on when it runs. Injecting a java.time.Clock and fixing it in tests, and seeding random generators, removes that.

Java
// Flaky: depends on when the test runs
boolean isToday(Order o) { return o.date().equals(LocalDate.now()); }
// Stable: the clock is a dependency
boolean isToday(Order o, Clock clock) { return o.date().equals(LocalDate.now(clock)); }
Clock fixed = Clock.fixed(Instant.parse("2026-03-29T00:30:00Z"), ZoneId.of("Europe/Berlin"));
// in tests we can now choose the awkward moments on purpose, like a DST change

Ordering assumptions are the third cause: a test that expects a HashMap or a database query without ORDER BY to return rows in a particular order, or tests that only pass when run in a certain sequence. The fourth is external services, real network calls to systems we do not control, which belong behind WireMock or Testcontainers. Asynchronous code checked with Thread.sleep() is the fifth; Awaitility with a time limit fixes it.

6Reproduce Locally Before Fixing

A fix that was never seen to fail is a guess. Before changing anything, we make the failure happen on demand. @RepeatedTest(200) on the flaky test, run locally, often makes it fail a few times, and the failure messages show which repetition and what went wrong. Running the test with a fixed clock set to the awkward time, under CPU stress, or together with the test that runs before it in CI are other ways to bring the failure out. When the test fails reliably before the fix and passes 200 times after it, we have real evidence.

7Follow-up questions

Is there ever a good reason to keep retries permanently?

For true end-to-end tests against shared environments, where a network blip outside our control can fail a test, a single retry with full reporting can be a reasonable cost. Even there, the retried tests must be counted, and a test that needs retries often is still treated as a problem to fix.

How do you tell a flaky test from a real regression?

By the history. A test that failed and then passed on the same commit is flaky. A test that starts failing on every run after a particular commit is a regression. Recording results per commit, which the reporting system does anyway, makes this a simple query.

Who should own fixing flaky tests?

The team that owns the code under test. A central team can build the reporting and quarantine tooling, but it cannot fix a race in someone else's service. The ticket, the deadline and the dashboard make the ownership concrete.

8Conclusion

In this answer, we saw that automatic retries turn intermittent failures into green builds, which hides real bugs such as races and time-zone problems and slows the pipeline. If retries are used, every test that passes only on retry must be reported as flaky with a ticket. Quarantine in a tagged, non-blocking job with an owner and a deadline keeps the pipeline reliable without losing the signal, and the lasting fix is to remove the common causes: shared state, uncontrolled time and randomness, ordering assumptions and external services, after reproducing the failure locally with @RepeatedTest.

What to say in the interviewAbout 80 seconds aloud

Open with

My main concern with automatic retries is that they hide real bugs. A flaky test is often a good test that found something intermittent, like a race condition or a time-zone problem near midnight, and those bugs happen in production too, where nothing retries.

Then, if they want more

Retries also make the pipeline slower, and because flakiness becomes cheap, the number of flaky tests grows.

So if we turn on something like Surefire's rerunFailingTestsCount, I would insist that any test that passed only on retry is reported as flaky, since it failed and passed on the same commit, and that a ticket is opened automatically for the owning team.

In addition, I would set up quarantine: known flaky tests get a flaky tag with a ticket in the reason, the main pipeline excludes that tag, and a separate non-blocking job still runs them so we keep the data. Each one has an owner and a deadline, after which it is fixed, rewritten or deleted.

Then the real work is fixing causes: isolating shared state, injecting a Clock and seeding randomness, removing ordering assumptions, replacing real external services with WireMock or Testcontainers, and replacing sleeps with Awaitility. And before fixing, I would reproduce the failure locally with @RepeatedTest, so the fix is proven rather than guessed.

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 team wants to add automatic retries for failing tests in CI (for example Surefire...": https://scrivio-interview-library.netlify.app/java/junit/1e1c3d00b5/