Timeout annotations help stop tests that take too long. This keeps your testing fast and reliable.
Timeout annotations in JUnit
@Test
@Timeout(value = 5, unit = TimeUnit.SECONDS)
public void testMethod() {
// test code here
}The @Timeout annotation sets a time limit for the test method.
If the test runs longer than the limit, it fails automatically.
@Test @Timeout(value = 2, unit = TimeUnit.SECONDS) public void quickTest() { // test that must finish within 2 seconds }
@Test @Timeout(1) public void defaultUnitTest() { // test with 1 second timeout (default unit is seconds) }
@Test @Timeout(value = 500, unit = TimeUnit.MILLISECONDS) public void fastTest() { // test must finish within 500 milliseconds }
The first test sleeps 1 second and passes because it is under the 2-second timeout.
The second test sleeps 2 seconds but has a 1-second timeout, so it fails.
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Timeout; import java.util.concurrent.TimeUnit; public class TimeoutExampleTest { @Test @Timeout(value = 2, unit = TimeUnit.SECONDS) public void testFastMethod() throws InterruptedException { Thread.sleep(1000); // sleeps 1 second, should pass } @Test @Timeout(value = 1, unit = TimeUnit.SECONDS) public void testSlowMethod() throws InterruptedException { Thread.sleep(2000); // sleeps 2 seconds, should fail } }
@Timeout can be used on test methods as well as @BeforeEach, @AfterEach, @BeforeAll, and @AfterAll methods.
Use timeouts carefully to avoid false failures on slow machines.
You can also set timeouts globally for all tests in some JUnit configurations.
Timeout annotations stop tests that run too long.
They help keep tests fast and catch hanging code.
Set time limits using @Timeout with a value and time unit.
Practice
@Timeout annotation in JUnit tests?Solution
Step 1: Understand the role of @Timeout
The @Timeout annotation sets a maximum time limit for a test method to run.Step 2: Identify what happens when time limit is exceeded
If the test runs longer than the specified time, it is stopped and marked as failed.Final Answer:
To stop a test if it runs longer than a specified time limit -> Option DQuick Check:
@Timeout stops long tests = A [OK]
- Confusing timeout with repeating tests
- Thinking timeout skips tests
- Assuming timeout marks tests as expected failures
Solution
Step 1: Recall JUnit 5 @Timeout syntax
The @Timeout annotation requires a value and a time unit specified as an enum from java.util.concurrent.TimeUnit.Step 2: Match correct syntax
@Timeout(value = 5, unit = TimeUnit.SECONDS) uses named parameters with value=5 and unit=TimeUnit.SECONDS, which is correct syntax.Final Answer:
@Timeout(value = 5, unit = TimeUnit.SECONDS) -> Option CQuick Check:
Use named params with TimeUnit enum = C [OK]
- Omitting the time unit
- Using wrong enum or missing TimeUnit prefix
- Passing timeout as single unnamed parameter
@Test
@Timeout(value = 2, unit = TimeUnit.SECONDS)
void testSleep() throws InterruptedException {
Thread.sleep(3000);
}What will happen when this test runs?Solution
Step 1: Analyze the timeout and sleep duration
The test has a timeout of 2 seconds but sleeps for 3 seconds, which is longer than allowed.Step 2: Understand test behavior on timeout
JUnit will stop the test after 2 seconds and mark it as failed due to timeout.Final Answer:
The test will fail due to timeout after 2 seconds -> Option AQuick Check:
Sleep longer than timeout causes failure = B [OK]
- Thinking test passes despite long sleep
- Assuming test fails immediately without waiting
- Believing test is skipped instead of failed
@Timeout(1) but does not compile. What is the error?@Test
@Timeout(1)
void testMethod() {
// test code
}Solution
Step 1: Check @Timeout annotation parameters
The @Timeout annotation requires both a value and a time unit parameter in JUnit 5.Step 2: Identify missing time unit
Using @Timeout(1) without specifying the time unit causes a compilation error.Final Answer:
Timeout annotation requires a time unit parameter -> Option BQuick Check:
Missing time unit parameter = A [OK]
- Forgetting to specify time unit
- Using @Timeout without time unit
- Thinking @Timeout only works on non-void methods
Solution
Step 1: Understand class-level @Timeout usage
JUnit 5 allows applying @Timeout at the class level to set a timeout for all test methods inside.Step 2: Compare options for efficiency
Adding @Timeout on the class is simpler and ensures all tests have the timeout without repeating annotation.Final Answer:
Add @Timeout(value = 500, unit = TimeUnit.MILLISECONDS) on the test class -> Option AQuick Check:
Class-level @Timeout applies to all tests = D [OK]
- Adding @Timeout repeatedly on each method
- Trying to set timeout via sleep calls
- Assuming global config sets timeout automatically
Ready to prove it?
Pass the test and this pattern moves unknown
