Class TimeoutRule

java.lang.Object
org.robolectric.junit.rules.TimeoutRule
All Implemented Interfaces:
org.junit.rules.TestRule

public class TimeoutRule extends Object implements org.junit.rules.TestRule
Robolectric's replacement for JUnit's Timeout.

Timeout spawns a new thread, which is not compatible with Robolectric's Scheduler. Instead this Rule uses TimeLimitedStatement like @Test(timeout=) does.

Example usage:

 @Rule public final TimeoutRule timeoutRule = TimeoutRule.seconds(40);

 @Test
  public void testWhichShouldFinishIn40Seconds() {
    // ...
  }
 
  • Constructor Summary

    Constructors
    Constructor
    Description
    TimeoutRule(long timeout, TimeUnit timeUnit)
    Create a TimeoutRule instance with the timeout specified at the timeUnit of granularity of the provided TimeUnit.
  • Method Summary

    Modifier and Type
    Method
    Description
    org.junit.runners.model.Statement
    apply(org.junit.runners.model.Statement base, org.junit.runner.Description description)
     
    millis(long millis)
    Creates a TimeoutRule that will timeout a test after the given duration, in milliseconds.
    seconds(long seconds)
    Creates a TimeoutRule that will timeout a test after the given duration, in seconds.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • TimeoutRule

      public TimeoutRule(long timeout, @Nonnull TimeUnit timeUnit)
      Create a TimeoutRule instance with the timeout specified at the timeUnit of granularity of the provided TimeUnit.
      Parameters:
      timeout - the maximum time to allow the test to run before it should timeout
      timeUnit - the time unit for the timeout
  • Method Details

    • apply

      public org.junit.runners.model.Statement apply(org.junit.runners.model.Statement base, org.junit.runner.Description description)
      Specified by:
      apply in interface org.junit.rules.TestRule
    • millis

      public static TimeoutRule millis(long millis)
      Creates a TimeoutRule that will timeout a test after the given duration, in milliseconds.
    • seconds

      public static TimeoutRule seconds(long seconds)
      Creates a TimeoutRule that will timeout a test after the given duration, in seconds.