Challenge - 5 Problems
Timeout Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
What is the result of this JUnit test using assertTimeout?
Consider the following JUnit 5 test code snippet. What will be the test execution result?
JUnit
import static org.junit.jupiter.api.Assertions.assertTimeout; import java.time.Duration; import org.junit.jupiter.api.Test; public class PerformanceTest { @Test void testFastOperation() { assertTimeout(Duration.ofMillis(100), () -> { Thread.sleep(50); }); } }
Attempts:
2 left
💡 Hint
assertTimeout checks if the code block finishes within the given duration.
✗ Incorrect
The test uses assertTimeout with 100 milliseconds. The code sleeps for 50 milliseconds, which is less than 100, so the test passes.
❓ assertion
intermediate2:00remaining
Which assertion correctly fails if the code block exceeds 200 milliseconds?
You want to fail a test if a code block takes longer than 200 milliseconds. Which JUnit assertion achieves this?
Attempts:
2 left
💡 Hint
assertTimeoutPreemptively interrupts the code if it exceeds the timeout.
✗ Incorrect
assertTimeout does not interrupt the code, so it waits for completion even if it exceeds timeout. assertTimeoutPreemptively fails immediately if the timeout is exceeded.
🔧 Debug
advanced2:00remaining
Why does this assertTimeout test fail even if the code is slow?
Analyze the following test code and explain why it fails, even if the code block takes longer than 1 second.
JUnit
import static org.junit.jupiter.api.Assertions.assertTimeout; import java.time.Duration; import org.junit.jupiter.api.Test; public class SlowTest { @Test void testSlowOperation() { assertTimeout(Duration.ofSeconds(1), () -> { Thread.sleep(1500); }); } }
Attempts:
2 left
💡 Hint
assertTimeout waits for the code block to finish before checking the elapsed time and fails if it exceeds the timeout.
✗ Incorrect
assertTimeout runs the code fully and then checks if it took longer than the timeout. It does not interrupt the code. So the test fails after 1.5 seconds because the duration exceeded the timeout.
🧠 Conceptual
advanced2:00remaining
What is the key difference between assertTimeout and assertTimeoutPreemptively in JUnit?
Choose the statement that best describes the difference between assertTimeout and assertTimeoutPreemptively.
Attempts:
2 left
💡 Hint
One assertion interrupts the code on timeout, the other does not.
✗ Incorrect
assertTimeout runs the code fully and then checks duration. assertTimeoutPreemptively interrupts the code execution if it exceeds the timeout, causing immediate failure.
❓ framework
expert3:00remaining
How to correctly use assertTimeoutPreemptively with a method that throws checked exceptions?
Given a method that throws InterruptedException, which is the correct way to use assertTimeoutPreemptively in JUnit 5 to test its performance?
JUnit
public void longRunningMethod() throws InterruptedException {
Thread.sleep(300);
}Attempts:
2 left
💡 Hint
Checked exceptions must be handled or declared in lambda expressions.
✗ Incorrect
Lambda passed to assertTimeoutPreemptively cannot throw checked exceptions directly. Wrapping the call in try-catch and rethrowing as unchecked exception is correct.