@Timeout is used to define a timeout for a method or all testable
methods within one class and its @Nested classes.
This annotation may also be used on lifecycle methods annotated with
@BeforeAll, @BeforeEach,
@AfterEach, or @AfterAll.
Applying this annotation to a test class has the same effect as applying
it to all testable methods, i.e. all methods annotated or meta-annotated with
@Test, @TestFactory, or
@TestTemplate, but not to its lifecycle methods.
If this annotation is not present, no timeout will be used unless a default timeout is defined via one of the following configuration parameters:
@Test methods@TestTemplate methods@TestFactory methods@BeforeAll methods@BeforeEach methods@AfterEach methods@AfterAll methodsMore specific configuration parameters override less specific ones. For example, junit.jupiter.execution.timeout.test.method.default overrides junit.jupiter.execution.timeout.testable.method.default which overrides junit.jupiter.execution.timeout.default.
Values for timeouts must be in the following, case-insensitive format:
<number> [ns|μs|ms|s|m|h|d]. The space between the number and the
unit may be omitted. Specifying no unit is equivalent to using seconds.
| Value | Equivalent annotation |
|---|---|
42 | @Timeout(42) |
42 ns | @Timeout(value = 42, unit = NANOSECONDS) |
42 μs | @Timeout(value = 42, unit = MICROSECONDS) |
42 ms | @Timeout(value = 42, unit = MILLISECONDS) |
42 s | @Timeout(value = 42, unit = SECONDS) |
42 m | @Timeout(value = 42, unit = MINUTES) |
42 h | @Timeout(value = 42, unit = HOURS) |
42 d | @Timeout(value = 42, unit = DAYS) |
You may use the junit.jupiter.execution.timeout.mode configuration parameter to explicitly enable or disable timeouts.
Supported values:
enabled: enables timeouts
disabled: disables timeouts
disabled_on_debug: disables timeouts while debugging
| Modifier and Type | Class and Description |
|---|---|
| public static enum | Timeout.
|
| Modifier and Type | Field and Description |
|---|---|
| public static final String | DEFAULT_AFTER_ALL_METHOD_TIMEOUT_PROPERTY_NAME
Property name used to set the default timeout for all
|
| public static final String | DEFAULT_AFTER_EACH_METHOD_TIMEOUT_PROPERTY_NAME
Property name used to set the default timeout for all
|
| public static final String | DEFAULT_BEFORE_ALL_METHOD_TIMEOUT_PROPERTY_NAME
Property name used to set the default timeout for all
|
| public static final String | DEFAULT_BEFORE_EACH_METHOD_TIMEOUT_PROPERTY_NAME
Property name used to set the default timeout for all
|
| public static final String | DEFAULT_LIFECYCLE_METHOD_TIMEOUT_PROPERTY_NAME
Property name used to set the default timeout for all lifecycle methods: . |
| public static final String | DEFAULT_TEST_FACTORY_METHOD_TIMEOUT_PROPERTY_NAME
Property name used to set the default timeout for all
|
| public static final String | DEFAULT_TEST_METHOD_TIMEOUT_PROPERTY_NAME
Property name used to set the default timeout for all |
| public static final String | DEFAULT_TEST_TEMPLATE_METHOD_TIMEOUT_PROPERTY_NAME
Property name used to set the default timeout for all
|
| public static final String | DEFAULT_TESTABLE_METHOD_TIMEOUT_PROPERTY_NAME
Property name used to set the default timeout for all testable methods: . |
| public static final String | DEFAULT_TIMEOUT_PROPERTY_NAME
Property name used to set the default timeout for all testable and lifecycle methods: . |
| public static final String | DEFAULT_TIMEOUT_THREAD_MODE_PROPERTY_NAME
Property name used to set the default thread mode for all testable and lifecycle methods: . |
| public static final String | TIMEOUT_MODE_PROPERTY_NAME
Property name used to configure whether timeouts are applied to tests: . |
| Modifier and Type | Method and Description |
|---|---|
| public Timeout. | |
| public TimeUnit | |
| public long |
| DEFAULT_AFTER_ALL_METHOD_TIMEOUT_PROPERTY_NAME | back to summary |
|---|---|
| public static final String DEFAULT_AFTER_ALL_METHOD_TIMEOUT_PROPERTY_NAME Property name used to set the default timeout for all
The value of this property will be used unless overridden by a
This property overrides the junit.jupiter.execution.timeout.lifecycle.method.default property. Please refer to the class description for the definition of supported values.
| |
| DEFAULT_AFTER_EACH_METHOD_TIMEOUT_PROPERTY_NAME | back to summary |
|---|---|
| public static final String DEFAULT_AFTER_EACH_METHOD_TIMEOUT_PROPERTY_NAME Property name used to set the default timeout for all
The value of this property will be used unless overridden by a
This property overrides the junit.jupiter.execution.timeout.lifecycle.method.default property. Please refer to the class description for the definition of supported values.
| |
| DEFAULT_BEFORE_ALL_METHOD_TIMEOUT_PROPERTY_NAME | back to summary |
|---|---|
| public static final String DEFAULT_BEFORE_ALL_METHOD_TIMEOUT_PROPERTY_NAME Property name used to set the default timeout for all
The value of this property will be used unless overridden by a
This property overrides the junit.jupiter.execution.timeout.lifecycle.method.default property. Please refer to the class description for the definition of supported values.
| |
| DEFAULT_BEFORE_EACH_METHOD_TIMEOUT_PROPERTY_NAME | back to summary |
|---|---|
| public static final String DEFAULT_BEFORE_EACH_METHOD_TIMEOUT_PROPERTY_NAME Property name used to set the default timeout for all
The value of this property will be used unless overridden by a
This property overrides the junit.jupiter.execution.timeout.lifecycle.method.default property. Please refer to the class description for the definition of supported values.
| |
| DEFAULT_LIFECYCLE_METHOD_TIMEOUT_PROPERTY_NAME | back to summary |
|---|---|
| public static final String DEFAULT_LIFECYCLE_METHOD_TIMEOUT_PROPERTY_NAME Property name used to set the default timeout for all lifecycle methods: . The value of this property will be used unless overridden by a more
specific property or a This property overrides the junit.jupiter.execution.timeout.default property. Please refer to the class description for the definition of supported values.
| |
| DEFAULT_TEST_FACTORY_METHOD_TIMEOUT_PROPERTY_NAME | back to summary |
|---|---|
| public static final String DEFAULT_TEST_FACTORY_METHOD_TIMEOUT_PROPERTY_NAME Property name used to set the default timeout for all
The value of this property will be used unless overridden by a
This property overrides the junit.jupiter.execution.timeout.testable.method.default property. Please refer to the class description for the definition of supported values.
| |
| DEFAULT_TEST_METHOD_TIMEOUT_PROPERTY_NAME | back to summary |
|---|---|
| public static final String DEFAULT_TEST_METHOD_TIMEOUT_PROPERTY_NAME Property name used to set the default timeout for all The value of this property will be used unless overridden by a
This property overrides the junit.jupiter.execution.timeout.testable.method.default property. Please refer to the class description for the definition of supported values.
| |
| DEFAULT_TEST_TEMPLATE_METHOD_TIMEOUT_PROPERTY_NAME | back to summary |
|---|---|
| public static final String DEFAULT_TEST_TEMPLATE_METHOD_TIMEOUT_PROPERTY_NAME Property name used to set the default timeout for all
The value of this property will be used unless overridden by a
This property overrides the junit.jupiter.execution.timeout.testable.method.default property. Please refer to the class description for the definition of supported values.
| |
| DEFAULT_TESTABLE_METHOD_TIMEOUT_PROPERTY_NAME | back to summary |
|---|---|
| public static final String DEFAULT_TESTABLE_METHOD_TIMEOUT_PROPERTY_NAME Property name used to set the default timeout for all testable methods: . The value of this property will be used unless overridden by a more
specific property or a This property overrides the junit.jupiter.execution.timeout.default property. Please refer to the class description for the definition of supported values.
| |
| DEFAULT_TIMEOUT_PROPERTY_NAME | back to summary |
|---|---|
| public static final String DEFAULT_TIMEOUT_PROPERTY_NAME Property name used to set the default timeout for all testable and lifecycle methods: . The value of this property will be used unless overridden by a more
specific property or a Please refer to the class description for the definition of supported values.
| |
| DEFAULT_TIMEOUT_THREAD_MODE_PROPERTY_NAME | back to summary |
|---|---|
| public static final String DEFAULT_TIMEOUT_THREAD_MODE_PROPERTY_NAME Property name used to set the default thread mode for all testable and lifecycle methods: . The value of this property will be used unless overridden by a
The supported values are
| |
| TIMEOUT_MODE_PROPERTY_NAME | back to summary |
|---|---|
| public static final String TIMEOUT_MODE_PROPERTY_NAME Property name used to configure whether timeouts are applied to tests: . The value of this property will be used to toggle whether
Supported timeout mode values (case insensitive):
If not specified, the default is
| |
| threadMode | back to summary |
|---|---|
| public Timeout. The thread mode of this timeout.
| |
| unit | back to summary |
|---|---|
| public TimeUnit unit() The time unit of this timeout. | |
| value | back to summary |
|---|---|
| public long value() The duration of this timeout.
| |
ThreadMode is used to define whether test code should be executed
in the thread of the calling code or in a separate thread.
| Modifier and Type | Field and Description |
|---|---|
| public static final Timeout. | INFERRED
The thread mode is determined using the parameter configured in property junit.jupiter.execution.timeout.thread.mode.default. |
| public static final Timeout. | SAME_THREAD
The test code is executed in the thread of the calling code. |
| public static final Timeout. | SEPARATE_THREAD
The test code is executed in a different thread than that of the calling code. |
| Access | Constructor and Description |
|---|---|
| private |
| Modifier and Type | Method and Description |
|---|---|
| public static Timeout. | |
| public static Timeout. |
| INFERRED | back to summary |
|---|---|
| public static final Timeout. The thread mode is determined using the parameter configured in property junit.jupiter.execution.timeout.thread.mode.default. | |
| SAME_THREAD | back to summary |
|---|---|
| public static final Timeout. The test code is executed in the thread of the calling code. | |
| SEPARATE_THREAD | back to summary |
|---|---|
| public static final Timeout. The test code is executed in a different thread than that of the calling code. Furthermore, execution of the test code will be preemptively aborted if the timeout is exceeded. See the Preemptive Timeouts section of the class-level Javadoc for a discussion of possible undesirable side effects. | |
| ThreadMode | back to summary |
|---|---|
| private ThreadMode() | |
| valueOf | back to summary |
|---|---|
| public static Timeout. | |
| values | back to summary |
|---|---|
| public static Timeout. | |