A flaky test is a test that sometimes passes and sometimes fails without any changes in the code. It behaves unpredictably, making it hard to trust test results.
Click to reveal answer
beginner
Why are flaky tests problematic in software testing?
Flaky tests cause confusion because they give inconsistent results. They can hide real bugs or waste time investigating false failures.
Click to reveal answer
beginner
Name one common cause of flaky tests.
One common cause is timing issues, like tests depending on slow network responses or asynchronous operations that are not properly handled.
Click to reveal answer
intermediate
How can you detect flaky tests using JUnit?
You can run the same test multiple times in JUnit and check if it sometimes fails and sometimes passes. Tools or plugins can help automate this detection.
Click to reveal answer
intermediate
What is a simple JUnit code example to run a test multiple times to detect flakiness?
Use @RepeatedTest annotation to run a test multiple times. If results vary, the test might be flaky.
Example:
import org.junit.jupiter.api.RepeatedTest;
import static org.junit.jupiter.api.Assertions.assertTrue;
class FlakyTestExample {
@RepeatedTest(5)
void testSometimesFails() {
boolean condition = Math.random() > 0.2; // 80% chance to pass
assertTrue(condition, "Test failed randomly");
}
}
Click to reveal answer
What does a flaky test do?
AAlways fails
BAlways passes
CRuns faster than other tests
DPasses and fails unpredictably
✗ Incorrect
A flaky test sometimes passes and sometimes fails without code changes.
Which of these is a common cause of flaky tests?
ATiming or race conditions
BClear assertions
CProperly isolated tests
DConsistent test data
✗ Incorrect
Timing or race conditions often cause flaky tests.
How can you detect flaky tests in JUnit?
ARun tests multiple times and check for inconsistent results
BRun tests once and trust results
CIgnore test failures
DOnly run tests manually
✗ Incorrect
Running tests multiple times helps find flaky tests by spotting inconsistent outcomes.
What JUnit annotation helps run a test multiple times?
A@Test
B@BeforeEach
C@RepeatedTest
D@AfterAll
✗ Incorrect
@RepeatedTest runs a test multiple times to check for flakiness.
Why should flaky tests be fixed?
AThey slow down the computer
BThey cause unreliable test results
CThey make tests run faster
DThey increase code coverage
✗ Incorrect
Flaky tests cause unreliable results, making it hard to trust testing.
Explain what a flaky test is and why it is important to detect it.
Think about tests that sometimes fail without code changes.
You got /4 concepts.
Describe a simple method to detect flaky tests using JUnit.
How can repeating a test help find flakiness?
You got /3 concepts.
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
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 B
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
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.
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
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 D
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
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 C
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
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 A
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 unknownstrongwhile earning +1 Grip toward your certificate.