Package org.junit.rules
Class Timeout
java.lang.Object
org.junit.rules.Timeout
- All Implemented Interfaces:
TestRule
The Timeout Rule applies the same timeout to all test methods in a class:
public static class HasGlobalLongTimeout { @Rule public Timeout globalTimeout = Timeout.millis(20); @Test public void run1() throws InterruptedException { Thread.sleep(100); } @Test public void infiniteLoop() { while (true) {} } }
Each test is run in a new thread. If the specified timeout elapses before
the test completes, its execution is interrupted via Thread.interrupt()
.
This happens in interruptable I/O and locks, and methods in Object
and Thread
throwing InterruptedException
.
A specified timeout of 0 will be interpreted as not set, however tests will still launch from separate threads. This can be useful for disabling timeouts in environments where they are dynamically set based on some property.
- Since:
- 4.7
-
Nested Class Summary
Nested Classes -
Field Summary
Fields -
Constructor Summary
ConstructorsModifierConstructorDescriptionTimeout
(int millis) Deprecated.Create aTimeout
instance with the timeout specified at the timeUnit of granularity of the providedTimeUnit
.protected
Timeout
(Timeout.Builder builder) Create aTimeout
instance initialized with values from a builder. -
Method Summary
Modifier and TypeMethodDescriptionapply
(Statement base, Description description) Modifies the method-runningStatement
to implement this test-running rule.static Timeout.Builder
builder()
Returns a new builder for building an instance.protected Statement
createFailOnTimeoutStatement
(Statement statement) Creates aStatement
that will run the givenstatement
, and timeout the operation based on the values configured in this rule.protected final boolean
Gets whether thisTimeout
will look for a stuck thread when the test times out.protected final long
getTimeout
(TimeUnit unit) Gets the timeout configured for this rule, in the given units.static Timeout
millis
(long millis) Creates aTimeout
that will timeout a test after the given duration, in milliseconds.static Timeout
seconds
(long seconds) Creates aTimeout
that will timeout a test after the given duration, in seconds.
-
Field Details
-
timeout
private final long timeout -
timeUnit
-
lookForStuckThread
private final boolean lookForStuckThread
-
-
Constructor Details
-
Timeout
Deprecated.Create aTimeout
instance with the timeout specified in milliseconds.This constructor is deprecated.
Instead use
Timeout(long, java.util.concurrent.TimeUnit)
,millis(long)
, orseconds(long)
.- Parameters:
millis
- the maximum time in milliseconds to allow the test to run before it should timeout
-
Timeout
Create aTimeout
instance with the timeout specified at the timeUnit of granularity of the providedTimeUnit
.- Parameters:
timeout
- the maximum time to allow the test to run before it should timeouttimeUnit
- the time unit for thetimeout
- Since:
- 4.12
-
Timeout
Create aTimeout
instance initialized with values from a builder.- Since:
- 4.12
-
-
Method Details
-
builder
Returns a new builder for building an instance.- Since:
- 4.12
-
millis
Creates aTimeout
that will timeout a test after the given duration, in milliseconds.- Since:
- 4.12
-
seconds
Creates aTimeout
that will timeout a test after the given duration, in seconds.- Since:
- 4.12
-
getTimeout
Gets the timeout configured for this rule, in the given units.- Since:
- 4.12
-
getLookingForStuckThread
protected final boolean getLookingForStuckThread()Gets whether thisTimeout
will look for a stuck thread when the test times out.- Since:
- 4.12
-
createFailOnTimeoutStatement
Creates aStatement
that will run the givenstatement
, and timeout the operation based on the values configured in this rule. Subclasses can override this method for different behavior.- Throws:
Exception
- Since:
- 4.12
-
apply
Description copied from interface:TestRule
Modifies the method-runningStatement
to implement this test-running rule.- Specified by:
apply
in interfaceTestRule
- Parameters:
base
- TheStatement
to be modifieddescription
- ADescription
of the test implemented inbase
- Returns:
- a new statement, which may be the same as
base
, a wrapper aroundbase
, or a completely new Statement.
-