0
0
JUnittesting~10 mins

Flaky test detection in JUnit - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to mark a test as flaky using JUnit 5.

JUnit
@Test
@[1](5)
void testRandomBehavior() {
    // test code here
}
Drag options to blanks, or click blank then click option'
AFlakyTest
BRepeatedTest
CTimeout
DDisabled
Attempts:
3 left
💡 Hint
Common Mistakes
Using @FlakyTest which does not exist in JUnit 5.
Using @Timeout which controls time limits, not repetitions.
2fill in blank
medium

Complete the code to assert that a flaky test passes at least once in multiple runs.

JUnit
int passCount = 0;
for (int i = 0; i < 10; i++) {
    try {
        flakyTest();
        passCount[1];
    } catch (Exception e) {
        // ignore failure
    }
}
assertTrue(passCount > 0);
Drag options to blanks, or click blank then click option'
A++
B--
C+=
D=
Attempts:
3 left
💡 Hint
Common Mistakes
Using '--' which decreases the count.
Using '=' which assigns instead of increments.
3fill in blank
hard

Fix the error in the flaky test retry logic by completing the blank.

JUnit
int retries = 3;
while (retries > 0) {
    try {
        flakyTest();
        break;
    } catch (Exception e) {
        retries[1];
        if (retries == 0) throw e;
    }
}
Drag options to blanks, or click blank then click option'
A=
B++
C+=
D--
Attempts:
3 left
💡 Hint
Common Mistakes
Using '++' which increases retries causing infinite loop.
Using '=' which assigns instead of decrementing.
4fill in blank
hard

Fill both blanks to create a JUnit 5 extension that retries flaky tests up to 3 times.

JUnit
public class RetryExtension implements TestExecutionExceptionHandler {
    private int maxRetries = [1];
    private int retryCount = 0;

    @Override
    public void handleTestExecutionException(ExtensionContext context, Throwable throwable) throws Throwable {
        if (retryCount < maxRetries) {
            retryCount[2];
            context.getRequiredTestMethod().invoke(context.getRequiredTestInstance());
        } else {
            throw throwable;
        }
    }
}
Drag options to blanks, or click blank then click option'
A3
B1
C++
D--
Attempts:
3 left
💡 Hint
Common Mistakes
Setting maxRetries to 1 which allows only one attempt.
Decrementing retryCount which causes logic errors.
5fill in blank
hard

Fill all three blanks to implement a flaky test detection method that runs a test 5 times and returns true if it passes at least once.

JUnit
public boolean isFlaky(Runnable test) {
    int passCount = 0;
    for (int i = 0; i < [1]; i++) {
        try {
            test.run();
            passCount [2];
        } catch (Exception e) {
            // ignore failure
        }
    }
    return passCount [3] 0;
}
Drag options to blanks, or click blank then click option'
A5
B++
C>
D<
Attempts:
3 left
💡 Hint
Common Mistakes
Using less than '<' instead of greater than '>' in the return statement.
Not incrementing passCount correctly.