Bird
Raised Fist0
JUnittesting~10 mins

Flaky test detection in JUnit - Test Execution Trace

Choose your learning style10 modes available
Test Overview

This test runs a simple method multiple times to detect if it sometimes fails unpredictably, which indicates a flaky test.

Test Code - JUnit 5
JUnit
import org.junit.jupiter.api.RepeatedTest;
import org.junit.jupiter.api.Assertions;
import java.util.Random;

public class FlakyTestDetection {

    private boolean sometimesFails() {
        // Simulate flaky behavior: fails randomly 30% of the time
        return new Random().nextInt(10) >= 7;
    }

    @RepeatedTest(10)
    void testSometimesFails() {
        Assertions.assertTrue(sometimesFails(), "Method failed unexpectedly");
    }
}
Execution Trace - 7 Steps
StepActionSystem StateAssertionResult
1Test runner starts and prepares to run testSometimesFails 10 timesJUnit test environment initialized—PASS
2Runs testSometimesFails iteration 1: calls sometimesFails()sometimesFails returns true (success)assertTrue passesPASS
3Runs testSometimesFails iteration 2: calls sometimesFails()sometimesFails returns true (success)assertTrue passesPASS
4Runs testSometimesFails iteration 3: calls sometimesFails()sometimesFails returns false (failure)assertTrue fails with message 'Method failed unexpectedly'FAIL
5Test runner records failure for iteration 3Test report marks iteration 3 as failed—PASS
6Runs testSometimesFails iteration 4 to 10: calls sometimesFails()sometimesFails returns true for most, possibly false for someassertTrue passes or fails accordinglyPASS OR FAIL DEPENDING ON ITERATION
7Test runner finishes all 10 iterations and aggregates resultsTest report shows some iterations passed, some failedOverall test marked as flaky due to intermittent failuresFAIL
Failure Scenario
Failing Condition: sometimesFails() returns false randomly causing assertion failure
Execution Trace Quiz - 3 Questions
Test your understanding
What does the @RepeatedTest(10) annotation do in this test?
ARuns 10 different test methods once each
BRuns the test method 10 times to detect flaky behavior
CRuns the test method once with 10 assertions
DRuns the test method 10 times in parallel threads
Key Result
Running tests multiple times helps detect flaky tests that fail unpredictably, allowing you to improve test reliability by stabilizing the code or environment.

Practice

(1/5)
1.

What is a flaky test in JUnit?

easy
A. A test that runs only once
B. A test that sometimes passes and sometimes fails without code changes
C. A test that always fails
D. A test that always passes

Solution

  1. 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.
  2. Step 2: Identify the correct definition

    Among the options, only D describes this behavior accurately.
  3. Final Answer:

    A test that sometimes passes and sometimes fails without code changes -> Option B
  4. Quick Check:

    Flaky test = inconsistent pass/fail [OK]
Hint: Flaky tests fail unpredictably without code changes [OK]
Common Mistakes:
  • Thinking flaky tests always fail
  • Confusing flaky tests with slow tests
  • Assuming flaky tests run only once
2.

Which JUnit feature helps detect flaky tests by rerunning failed tests automatically?

easy
A. JUnit RetryRule or @RepeatedTest
B. JUnit @BeforeAll annotation
C. JUnit @Test timeout parameter
D. JUnit @Ignore annotation

Solution

  1. Step 1: Identify rerun features in JUnit

    JUnit provides RetryRule or can use @RepeatedTest to rerun tests multiple times to detect flaky behavior.
  2. 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.
  3. Final Answer:

    JUnit RetryRule or @RepeatedTest -> Option A
  4. Quick Check:

    RetryRule reruns tests to detect flakes [OK]
Hint: RetryRule reruns failed tests automatically [OK]
Common Mistakes:
  • Confusing @BeforeAll with retry logic
  • Using @Ignore to detect flaky tests
  • Thinking timeout detects flakiness
3.

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?

medium
A. Test fails after all retries
B. Test fails immediately
C. Test passes on first try
D. Test passes after retrying once

Solution

  1. 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.
  2. Step 2: Check retry behavior

    On second run, runCount is 2, assertion passes, so test passes after one retry.
  3. Final Answer:

    Test passes after retrying once -> Option D
  4. Quick Check:

    Retry allows flaky test to pass on retry [OK]
Hint: Check runCount and retries to predict pass/fail [OK]
Common Mistakes:
  • Assuming test passes on first try
  • Ignoring retry rule effect
  • Thinking test fails after all retries
4.

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);
}
medium
A. assertTrue condition is always true
B. RetryRule is not applied correctly without @Retry annotation
C. The count variable resets on each test run, so retry never passes
D. Test method lacks @Before annotation

Solution

  1. Step 1: Analyze count variable scope

    Count is a local variable inside the test method, so it resets to 0 on every retry.
  2. 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.
  3. Final Answer:

    The count variable resets on each test run, so retry never passes -> Option C
  4. Quick Check:

    Local variables reset each retry causing failure [OK]
Hint: Check variable scope to ensure state persists across retries [OK]
Common Mistakes:
  • Assuming retry fixes all failures
  • Ignoring variable reset on retries
  • Confusing retry annotations
5.

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?

hard
A. Use @RepeatedTest(5) and record failures to identify flaky tests
B. Use @Ignore on all tests and run manually
C. Use @BeforeEach to reset test state only
D. Use @Timeout to fail slow tests

Solution

  1. Step 1: Understand flaky test detection by multiple runs

    Running tests multiple times helps catch intermittent failures indicating flakiness.
  2. Step 2: Choose correct JUnit feature

    @RepeatedTest(5) runs the test 5 times; by recording failures during these runs, flaky tests can be detected.
  3. Final Answer:

    Use @RepeatedTest(5) and record failures to identify flaky tests -> Option A
  4. Quick Check:

    Multiple runs with @RepeatedTest detect flaky tests [OK]
Hint: Run tests multiple times with @RepeatedTest to find flakes [OK]
Common Mistakes:
  • Using @Ignore disables tests instead of detecting flakes
  • Relying on @Timeout for flaky detection
  • Resetting state without multiple runs
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard