0
0
JUnittesting~20 mins

assertTimeout for performance in JUnit - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Timeout Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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);
        });
    }
}
ATest fails because the operation exceeds the 100 milliseconds timeout.
BTest fails because Thread.sleep throws InterruptedException.
CTest passes because the operation completes within 100 milliseconds.
DTest is skipped due to missing @Timeout annotation.
Attempts:
2 left
💡 Hint
assertTimeout checks if the code block finishes within the given duration.
assertion
intermediate
2: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?
AassertTimeoutPreemptively(Duration.ofMillis(200), () -> { Thread.sleep(250); });
BassertTimeout(Duration.ofMillis(200), () -> { Thread.sleep(100); });
CassertTimeout(Duration.ofMillis(200), () -> { Thread.sleep(150); });
DassertTimeoutPreemptively(Duration.ofMillis(200), () -> { Thread.sleep(150); });
Attempts:
2 left
💡 Hint
assertTimeoutPreemptively interrupts the code if it exceeds the timeout.
🔧 Debug
advanced
2: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);
        });
    }
}
AassertTimeoutPreemptively should be used to fail immediately on timeout, not assertTimeout.
BassertTimeout waits for the code to finish and then checks duration, so test fails after completion.
CThread.sleep throws an exception that is caught by assertTimeout, so test passes.
DThe test is missing @Timeout annotation, so timeout is ignored.
Attempts:
2 left
💡 Hint
assertTimeout waits for the code block to finish before checking the elapsed time and fails if it exceeds the timeout.
🧠 Conceptual
advanced
2:00remaining
What is the key difference between assertTimeout and assertTimeoutPreemptively in JUnit?
Choose the statement that best describes the difference between assertTimeout and assertTimeoutPreemptively.
AassertTimeout waits for the code block to finish before checking timeout; assertTimeoutPreemptively interrupts the code if timeout is exceeded.
BassertTimeoutPreemptively waits for completion; assertTimeout interrupts the code on timeout.
CBoth assertions behave the same and do not interrupt the code block.
DassertTimeoutPreemptively only works with asynchronous code; assertTimeout works with synchronous code.
Attempts:
2 left
💡 Hint
One assertion interrupts the code on timeout, the other does not.
framework
expert
3: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);
}
AassertTimeoutPreemptively(Duration.ofMillis(500), () -> longRunningMethod());
BassertTimeoutPreemptively(Duration.ofMillis(500), () -> { longRunningMethod(); });
CassertTimeoutPreemptively(Duration.ofMillis(500), () -> { longRunningMethod() throws InterruptedException; });
DassertTimeoutPreemptively(Duration.ofMillis(500), () -> { try { longRunningMethod(); } catch (InterruptedException e) { throw new RuntimeException(e); } });
Attempts:
2 left
💡 Hint
Checked exceptions must be handled or declared in lambda expressions.