@Timeout is a JUnit 5 annotation (Jupiter) that makes a test, a lifecycle method or a whole class fail if it runs longer than a given duration. It is declared in org.junit.jupiter.api.Timeout and has been available since JUnit 5.5, and it carries over to later versions including JUnit 6.
It protects your build from hanging on a deadlock, an infinite loop or a stuck network call, because the test fails with a TimeoutException instead of blocking the pipeline.
The value is a number and the unit defaults to seconds.
The second test fails with a message similar to failsBecauseItIsTooSlow() timed out after 200 milliseconds. By default the test runs on the same thread as the engine, and JUnit interrupts that thread when the time is over.
| Location | Effect |
|---|---|
@Test, @RepeatedTest, @ParameterizedTest method | Applies to that test, and each invocation is timed separately |
@TestFactory, @TestTemplate | Applies to the factory or template method |
@BeforeEach, @AfterEach, @BeforeAll, @AfterAll | Applies to that lifecycle method |
| Test class | Applies to every test method in the class |
@Nested class | Applies to its tests and is inherited from the outer class |
A method-level annotation overrides the class-level one. Placing @Timeout on a class applies the same limit to each test individually, not to the class as a whole.
Since JUnit 5.9 you can choose how the timeout is enforced with the threadMode attribute.
SAME_THREAD (the default): the test runs on the calling thread, and a watchdog interrupts it. This is required for tests that use thread-bound resources such as ThreadLocal, Spring transactions or database connections tied to the thread.SEPARATE_THREAD: the test runs in another thread and the engine fails the test at the deadline, even if the code ignores interrupts. The thread may keep running in the background.INFERRED: the default of the global configuration, which resolves to the same thread unless you configure otherwise.A code path that never checks for interrupts (a tight loop) cannot be stopped in SAME_THREAD mode, so the test is reported as failed only after it ends.
Instead of repeating annotations, set a default in junit-platform.properties under src/test/resources:
The value accepts a number with an optional unit: ns, us, ms, s, m and h. More specific properties such as testable.method.default, testtemplate.method.default and beforeall.method.default override the general default. An annotation on the element always wins over the configuration.
The junit.jupiter.execution.timeout.mode property accepts enabled, disabled and disabled_on_debug. The last one is useful because a breakpoint would otherwise trigger a timeout while you debug.
Assertions.assertTimeout and assertTimeoutPreemptively limit a block of code inside a test:
Use @Timeout to set a limit for the whole test or setup method, and use assertTimeout for one statement in the middle. assertTimeout waits until the code finishes and then fails if it took too long, while assertTimeoutPreemptively runs the code in another thread and aborts it at the deadline.
Thread.sleep when you wait for asynchronous results.Seconds. Change it with the unit attribute, for example unit = TimeUnit.MILLISECONDS.
No. JUnit 4 uses @Test(timeout = ...) or the Timeout rule. @Timeout is part of JUnit Jupiter.
A java.util.concurrent.TimeoutException reports the failure, with a message that names the test and the limit.
Yes, @Timeout works with @SpringBootTest too. Use the default SAME_THREAD mode, so that transactional test behaviour remains correct.
@Timeout fails any test, lifecycle method or class of tests that exceeds a time limit. The default unit is seconds, the thread mode controls how a stuck test is stopped, and global defaults can be set in junit-platform.properties. Use it to prevent hanging builds, with realistic limits for your CI hardware.