Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using @FlakyTest which does not exist in JUnit 5.
Using @Timeout which controls time limits, not repetitions.
✗ Incorrect
JUnit 5 uses @RepeatedTest to run a test multiple times to detect flaky behavior.
2fill in blank
mediumComplete 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using '--' which decreases the count.
Using '=' which assigns instead of increments.
✗ Incorrect
Using '++' increments passCount by one each time the test passes.
3fill in blank
hardFix 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using '++' which increases retries causing infinite loop.
Using '=' which assigns instead of decrementing.
✗ Incorrect
Decrement retries with '--' to limit retry attempts.
4fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Setting maxRetries to 1 which allows only one attempt.
Decrementing retryCount which causes logic errors.
✗ Incorrect
maxRetries is set to 3 and retryCount is incremented with '++' on each retry.
5fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using less than '<' instead of greater than '>' in the return statement.
Not incrementing passCount correctly.
✗ Incorrect
Run test 5 times, increment passCount with '++', and check if passCount is greater than 0.