Challenge - 5 Problems
Timeout Annotation Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
JUnit Timeout Annotation Basic Behavior
What is the test result when the following JUnit 5 test method runs?
JUnit
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Timeout; import java.util.concurrent.TimeUnit; public class TimeoutTest { @Test @Timeout(value = 1, unit = TimeUnit.SECONDS) void testTimeout() throws InterruptedException { Thread.sleep(1500); } }
Attempts:
2 left
💡 Hint
The test sleeps longer than the timeout value.
✗ Incorrect
The @Timeout annotation causes the test to fail if it runs longer than the specified time. Here, the test sleeps for 1.5 seconds but timeout is 1 second, so it fails.
❓ assertion
intermediate2:00remaining
Correct Usage of Timeout Annotation Units
Which of the following JUnit 5 timeout annotations correctly sets a timeout of 500 milliseconds?
Attempts:
2 left
💡 Hint
Check the unit parameter and value type.
✗ Incorrect
The value parameter is a long integer, so 500 milliseconds must be specified as value=500 with unit=TimeUnit.MILLISECONDS. Option D uses a double which is invalid, A misses unit so defaults to seconds, and D sets 500 seconds which is too long.
🔧 Debug
advanced2:00remaining
Fixing Timeout Annotation Misuse
Given this JUnit 5 test method, what error will occur and why?
@Test
@Timeout(1.5)
void testMethod() throws InterruptedException {
Thread.sleep(1000);
}
JUnit
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Timeout; public class TimeoutDebug { @Test @Timeout(1.5) void testMethod() throws InterruptedException { Thread.sleep(1000); } }
Attempts:
2 left
💡 Hint
Check the type of the value parameter in @Timeout.
✗ Incorrect
The @Timeout annotation's value parameter expects a long integer, but 1.5 is a double literal, causing a compilation error.
🧠 Conceptual
advanced2:00remaining
Timeout Annotation Scope in JUnit 5
Which statement correctly describes the scope of the @Timeout annotation in JUnit 5?
Attempts:
2 left
💡 Hint
Think about how annotations can be inherited in JUnit 5.
✗ Incorrect
In JUnit 5, @Timeout can be placed on a test method to affect that method or on a test class to apply the timeout to all test methods in that class.
❓ framework
expert2:00remaining
Behavior of Timeout with Nested Tests in JUnit 5
Consider a JUnit 5 test class with nested test classes. If @Timeout is applied on the outer class, what happens to the nested test methods?
JUnit
import org.junit.jupiter.api.Nested; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Timeout; import java.util.concurrent.TimeUnit; @Timeout(value = 1, unit = TimeUnit.SECONDS) public class OuterTimeoutTest { @Test void outerTest() throws InterruptedException { Thread.sleep(900); } @Nested class InnerTests { @Test void innerTest() throws InterruptedException { Thread.sleep(1100); } } }
Attempts:
2 left
💡 Hint
Check how annotations are inherited by nested classes in JUnit 5.
✗ Incorrect
In JUnit 5, annotations like @Timeout on an outer test class apply to all nested test classes and their methods. Thus, innerTest is subject to the 1 second timeout and fails because it sleeps 1100 ms.