程序包 org.junit.rules
类 Timeout
- java.lang.Object
- org.junit.rules.Timeout
- 所有已实现的接口:
TestRule
public class Timeout extends Object implements TestRule
The Timeout Rule applies the same timeout to all test methods in a class:public static class HasGlobalLongTimeout { @Rule public Timeout globalTimeout= new Timeout(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 inObject
andThread
throwingInterruptedException
.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.
- 从以下版本开始:
- 4.7
嵌套类概要
嵌套类 修饰符和类型 类 说明 static class
Timeout.Builder
Builder forTimeout
.
构造器概要
构造器 限定符 构造器 说明 Timeout(int millis)
已过时。Timeout(long timeout, TimeUnit timeUnit)
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 form a builder.
方法概要
所有方法 静态方法 实例方法 具体方法 修饰符和类型 方法 说明 Statement
apply(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 boolean
getLookingForStuckThread()
Gets whether thisTimeout
will look for a stuck thread when the test times out.protected 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.
构造器详细资料
Timeout
@Deprecated public Timeout(int millis)
已过时。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)
.- 参数:
millis
- the maximum time in milliseconds to allow the test to run before it should timeout
Timeout
public Timeout(long timeout, TimeUnit timeUnit)
Create aTimeout
instance with the timeout specified at the timeUnit of granularity of the providedTimeUnit
.- 参数:
timeout
- the maximum time to allow the test to run before it should timeouttimeUnit
- the time unit for thetimeout
- 从以下版本开始:
- 4.12
Timeout
protected Timeout(Timeout.Builder builder)
Create aTimeout
instance initialized with values form a builder.- 从以下版本开始:
- 4.12
方法详细资料
builder
public static Timeout.Builder builder()
Returns a new builder for building an instance.- 从以下版本开始:
- 4.12
millis
public static Timeout millis(long millis)
Creates aTimeout
that will timeout a test after the given duration, in milliseconds.- 从以下版本开始:
- 4.12
seconds
public static Timeout seconds(long seconds)
Creates aTimeout
that will timeout a test after the given duration, in seconds.- 从以下版本开始:
- 4.12
getTimeout
protected final long getTimeout(TimeUnit unit)
Gets the timeout configured for this rule, in the given units.- 从以下版本开始:
- 4.12
getLookingForStuckThread
protected final boolean getLookingForStuckThread()
Gets whether thisTimeout
will look for a stuck thread when the test times out.- 从以下版本开始:
- 4.12
createFailOnTimeoutStatement
protected Statement createFailOnTimeoutStatement(Statement statement) throws Exception
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.- 抛出:
Exception
- 从以下版本开始:
- 4.12
apply
public Statement apply(Statement base, Description description)
从接口复制的说明:TestRule
Modifies the method-runningStatement
to implement this test-running rule.- 指定者:
apply
在接口中TestRule
- 参数:
base
- TheStatement
to be modifieddescription
- ADescription
of the test implemented inbase
- 返回:
- a new statement, which may be the same as
base
, a wrapper aroundbase
, or a completely new Statement.