0
0
JUnittesting~20 mins

Timeout annotations in JUnit - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Timeout Annotation Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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);
    }
}
ATest passes successfully
BTest fails due to assertion error
CTest fails due to timeout exception after 1 second
DTest is skipped
Attempts:
2 left
💡 Hint
The test sleeps longer than the timeout value.
assertion
intermediate
2:00remaining
Correct Usage of Timeout Annotation Units
Which of the following JUnit 5 timeout annotations correctly sets a timeout of 500 milliseconds?
A@Timeout(value = 500)
B@Timeout(value = 0.5, unit = TimeUnit.SECONDS)
C@Timeout(value = 500, unit = TimeUnit.SECONDS)
D@Timeout(value = 500, unit = TimeUnit.MILLISECONDS)
Attempts:
2 left
💡 Hint
Check the unit parameter and value type.
🔧 Debug
advanced
2: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);
    }
}
ATest fails due to timeout
BCompilation error: The value must be a long, not a double
CTest passes successfully
DRuntime error: IllegalArgumentException
Attempts:
2 left
💡 Hint
Check the type of the value parameter in @Timeout.
🧠 Conceptual
advanced
2:00remaining
Timeout Annotation Scope in JUnit 5
Which statement correctly describes the scope of the @Timeout annotation in JUnit 5?
A@Timeout can be applied at method and class level, affecting all test methods in the class
B@Timeout only works on individual test methods, not on classes
C@Timeout applies globally to all tests in the project without annotation
D@Timeout only works on test classes annotated with @Timeoutable
Attempts:
2 left
💡 Hint
Think about how annotations can be inherited in JUnit 5.
framework
expert
2: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);
        }
    }
}
ATimeout applies to both outer and nested test methods, so innerTest fails due to timeout
BTimeout applies only to outerTest; nested tests have no timeout
CTimeout applies only to nested tests, not outerTest
DTimeout is ignored for all tests because of nesting
Attempts:
2 left
💡 Hint
Check how annotations are inherited by nested classes in JUnit 5.