Flaky tests sometimes pass and sometimes fail without code changes. Detecting them helps keep tests reliable and trustworthy.
Flaky test detection in JUnit
Use repeated test runs or JUnit extensions to detect flaky tests. Example with JUnit 5 @RepeatedTest: @RepeatedTest(5) void testSomething() { // test code } Or use a custom extension to rerun failed tests automatically.
JUnit 5 supports @RepeatedTest to run the same test multiple times.
Flaky test detection often involves running tests multiple times and checking for inconsistent results.
@RepeatedTest(3) void testRandomBehavior() { int value = (int)(Math.random() * 10); assertTrue(value < 8); }
import org.junit.jupiter.api.extension.ExtensionContext; import org.junit.jupiter.api.extension.TestExecutionExceptionHandler; import java.lang.reflect.InvocationTargetException; public class RetryExtension implements TestExecutionExceptionHandler { private int maxRetries = 3; private int retryCount = 0; @Override public void handleTestExecutionException(ExtensionContext context, Throwable throwable) throws Throwable { if (retryCount < maxRetries) { retryCount++; try { context.getRequiredTestMethod().invoke(context.getRequiredTestInstance()); } catch (InvocationTargetException e) { handleTestExecutionException(context, e.getCause()); } } else { throw throwable; } } }
This test runs 5 times. It prints the random number each time. If the number is 8 or 9, the test fails, showing flakiness.
import static org.junit.jupiter.api.Assertions.assertTrue; import org.junit.jupiter.api.RepeatedTest; public class FlakyTestExample { @RepeatedTest(5) void testRandomNumber() { int number = (int)(Math.random() * 10); System.out.println("Test run with number: " + number); assertTrue(number < 8, "Number is too high, test fails"); } }
Flaky tests can hide real bugs or waste time. Detecting them early is important.
Running tests multiple times helps reveal flaky behavior caused by timing or randomness.
Use logging or print statements to see test behavior during repeated runs.
Flaky tests sometimes pass and sometimes fail without code changes.
Detect flaky tests by running them multiple times or using retry extensions.
Fix flaky tests to keep your test suite reliable and trustworthy.
Practice
What is a flaky test in JUnit?
Solution
Step 1: Understand flaky test behavior
A flaky test is one that does not have consistent results; it may pass or fail unpredictably even if the code hasn't changed.Step 2: Identify the correct definition
Among the options, only D describes this behavior accurately.Final Answer:
A test that sometimes passes and sometimes fails without code changes -> Option BQuick Check:
Flaky test = inconsistent pass/fail [OK]
- Thinking flaky tests always fail
- Confusing flaky tests with slow tests
- Assuming flaky tests run only once
Which JUnit feature helps detect flaky tests by rerunning failed tests automatically?
Solution
Step 1: Identify rerun features in JUnit
JUnit provides RetryRule or can use @RepeatedTest to rerun tests multiple times to detect flaky behavior.Step 2: Eliminate unrelated options
@BeforeAll runs setup once, @Test timeout limits test time, and @Ignore skips tests, so they don't detect flaky tests.Final Answer:
JUnit RetryRule or @RepeatedTest -> Option AQuick Check:
RetryRule reruns tests to detect flakes [OK]
- Confusing @BeforeAll with retry logic
- Using @Ignore to detect flaky tests
- Thinking timeout detects flakiness
Consider this JUnit test snippet using a retry rule to detect flaky tests:
public class FlakyTest {
private int runCount = 0;
@Rule
public RetryRule retryRule = new RetryRule(3);
@Test
public void testFlaky() {
runCount++;
assertTrue(runCount >= 2);
}
}What will be the test result when run?
Solution
Step 1: Analyze runCount increment and assertion
On first run, runCount is 1, so assertTrue(runCount >= 2) fails. RetryRule reruns test up to 3 times.Step 2: Check retry behavior
On second run, runCount is 2, assertion passes, so test passes after one retry.Final Answer:
Test passes after retrying once -> Option DQuick Check:
Retry allows flaky test to pass on retry [OK]
- Assuming test passes on first try
- Ignoring retry rule effect
- Thinking test fails after all retries
Identify the problem in this JUnit test intended to detect flaky tests by retrying:
@Rule
public RetryRule retryRule = new RetryRule(3);
@Test
public void testFlaky() {
int count = 0;
count++;
assertTrue(count > 1);
}Solution
Step 1: Analyze count variable scope
Count is a local variable inside the test method, so it resets to 0 on every retry.Step 2: Understand retry effect on count
Since count resets each time, count++ makes it 1, so assertTrue(count > 1) always fails, retry won't help.Final Answer:
The count variable resets on each test run, so retry never passes -> Option CQuick Check:
Local variables reset each retry causing failure [OK]
- Assuming retry fixes all failures
- Ignoring variable reset on retries
- Confusing retry annotations
You want to detect flaky tests in your JUnit suite by running each test 5 times and marking it flaky if it fails at least once. Which approach is best?
Solution
Step 1: Understand flaky test detection by multiple runs
Running tests multiple times helps catch intermittent failures indicating flakiness.Step 2: Choose correct JUnit feature
@RepeatedTest(5) runs the test 5 times; by recording failures during these runs, flaky tests can be detected.Final Answer:
Use @RepeatedTest(5) and record failures to identify flaky tests -> Option AQuick Check:
Multiple runs with @RepeatedTest detect flaky tests [OK]
- Using @Ignore disables tests instead of detecting flakes
- Relying on @Timeout for flaky detection
- Resetting state without multiple runs
Ready to prove it?
Pass the test and this pattern moves unknown
