0
0
JUnittesting~20 mins

Flaky test detection in JUnit - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Flaky Test Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
What is a flaky test?

In software testing, what best describes a flaky test?

AA test that sometimes passes and sometimes fails without code changes
BA test that always passes regardless of code changes
CA test that never runs due to configuration errors
DA test that only runs on weekends
Attempts:
2 left
💡 Hint

Think about tests that behave unpredictably.

Predict Output
intermediate
2:00remaining
JUnit test with flaky behavior output

What will be the output of this JUnit test if it is flaky due to timing?

JUnit
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertTrue;

public class FlakyTest {
    @Test
    void testRandomPass() throws InterruptedException {
        Thread.sleep((long)(Math.random() * 100));
        assertTrue(System.currentTimeMillis() % 2 == 0);
    }
}
ATest sometimes passes and sometimes fails
BTest fails every time
CTest passes every time
DCompilation error due to Thread.sleep
Attempts:
2 left
💡 Hint

Look at the assertion and the use of random timing.

assertion
advanced
2:00remaining
Identify the assertion that detects flaky test behavior

Which assertion best helps detect flaky tests by running the same test multiple times?

AassertEquals(expected, actual)
BassertTrue(runTestMultipleTimes() >= 1)
CassertDoesNotThrow(() -> method())
DassertTimeout(Duration.ofSeconds(1), () -> method())
Attempts:
2 left
💡 Hint

Consider how to check if a test passes at least once in multiple runs.

🔧 Debug
advanced
2:00remaining
Debugging a flaky test caused by shared state

Given this JUnit test code, what is the most likely cause of flakiness?

JUnit
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertTrue;

public class SharedStateTest {
    private static int counter = 0;

    @Test
    void testIncrement() {
        counter++;
        assertTrue(counter <= 1);
    }
}
AassertTrue condition is always true
Bcounter variable is local and resets each test
CTest method is missing @BeforeEach annotation
Dcounter is not reset between tests causing shared state issues
Attempts:
2 left
💡 Hint

Think about static variables and test isolation.

framework
expert
2:00remaining
Using JUnit 5 to detect flaky tests automatically

Which JUnit 5 feature helps detect flaky tests by repeating a test multiple times and reporting failures?

A@BeforeAll annotation for setup before all tests
B@Test annotation with timeout parameter
C@RepeatedTest annotation with a fixed number of repetitions
D@Disabled annotation to skip flaky tests
Attempts:
2 left
💡 Hint

Look for annotations that run tests multiple times.