JUnit @Test(timeout) exception timing relative to test method completion
0 reputation · 20 Feb 2026, 05:20 UTC
The @Test(timeout) annotation in JUnit establishes a best-effort execution limit for test methods, throwing TestTimeoutException when the configured threshold is exceeded. While the annotation's intent is well-documented, the precise conditions governing exception delivery relative to test method termination remain ambiguous across major versions. Thread interruption mechanisms and scheduler delays can cause the exception to fire after the test method's logical conclusion, and the timeout scope--per-method in JUnit 4 versus per-invocation in JUnit 5--affects nested, parameterized, and class-level test configurations differently. This behavioral gap influences test reliability when deterministic resource cleanup or accurate execution-time reporting is required.
- Does the JUnit 5
Timeoutextension respect the same thread-interruption model as the@Test(timeout)attribute for dynamically discovered test instances? - How does the timeout boundary interact with
@BeforeEachand@AfterEachlifecycle methods when the test method exceeds the configured limit? - In JUnit 4, does a
TestTimeoutExceptionprevent@Aftermethod execution inTestWatcherorRunListenerhooks?