Complete the code to mark a test as flaky using JUnit 5.
@Test @[1](5) void testRandomBehavior() { // test code here }
Complete the code to assert that a flaky test passes at least once in multiple runs.
int passCount = 0; for (int i = 0; i < 10; i++) { try { flakyTest(); passCount[1]; } catch (Exception e) { // ignore failure } } assertTrue(passCount > 0);
Fix the error in the flaky test retry logic by completing the blank.
int retries = 3; while (retries > 0) { try { flakyTest(); break; } catch (Exception e) { retries[1]; if (retries == 0) throw e; } }
Fill both blanks to create a JUnit 5 extension that retries flaky tests up to 3 times.
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; } } }
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.
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;
}