0
0
JUnittesting~5 mins

Flaky test detection in JUnit - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is a flaky test?
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
Which of these is a common cause of flaky tests?
ATiming or race conditions
BClear assertions
CProperly isolated tests
DConsistent test data
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
What JUnit annotation helps run a test multiple times?
A@Test
B@BeforeEach
C@RepeatedTest
D@AfterAll
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
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.