# UiThreadTestRule

Summary:[Ctors](https://developer.android.com/reference/android/support/test/rule/UiThreadTestRule#pubctors)\|[Methods](https://developer.android.com/reference/android/support/test/rule/UiThreadTestRule#pubmethods)\|[Protected Methods](https://developer.android.com/reference/android/support/test/rule/UiThreadTestRule#promethods)\|[Inherited Methods](https://developer.android.com/reference/android/support/test/rule/UiThreadTestRule#inhmethods)\|[\[Expand All\]](https://developer.android.com/reference/android/support/test/rule/UiThreadTestRule#)  

# UiThreadTestRule

| This package is part of the Android[support library](https://developer.android.com/topic/libraries/support-library)which is no longer maintained. The support library has been superseded by[AndroidX](https://developer.android.com/jetpack/androidx)which is part of[Jetpack](https://developer.android.com/jetpack). We recommend using the AndroidX libraries in all new projects. You should also consider[migrating](https://developer.android.com/jetpack/androidx/migrate)existing projects to AndroidX. To find the AndroidX class that maps to this deprecated class, see the AndroidX support library[class mappings](https://developer.android.com/jetpack/androidx/migrate/class-mappings).

`
public


class
UiThreadTestRule
`  
`

extends `[Object](https://developer.android.com/reference/java/lang/Object.html)`


``


implements

TestRule


`

|---|--------------------------------------------|
| [java.lang.Object](https://developer.android.com/reference/java/lang/Object.html) ||
| ↳ | android.support.test.rule.UiThreadTestRule |

<br />

*** ** * ** ***

<br />

**This class is deprecated.**   
use[UiThreadTest](https://developer.android.com/reference/android/support/test/annotation/UiThreadTest)directly without this rule.[UiThreadTest](https://developer.android.com/reference/android/support/test/annotation/UiThreadTest)is now supported as part of the core Android test runner to provide the ability to run methods annotated with`@Before`and`@After`on the UI thread regardless of what`@Test`is annotated with.

This rule allows the test method annotated with[UiThreadTest](https://developer.android.com/reference/android/support/test/annotation/UiThreadTest)to execute on the application's main thread (or UI thread).

Note, methods annotated with[`Before`](http://junit.source.net/javadoc/org/junit/Before.html)and[`After`](http://junit.source.net/javadoc/org/junit/After.html)will also be executed on the UI thread.  
**See also:**

- [if you need to switch in and out of the UI
  thread within your method.](https://developer.android.com/reference/android/support/test/annotation/UiThreadTest)

## Summary

|                                                       ### Public constructors                                                        ||
|-----------------------------------------------------------------------------------------------------------------------------------|---|
| ` `[UiThreadTestRule](https://developer.android.com/reference/android/support/test/rule/UiThreadTestRule#UiThreadTestRule())`() ` |

|                                                                                                                                               ### Public methods                                                                                                                                               ||
|--------------|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| ` Statement` | ` `[apply](https://developer.android.com/reference/android/support/test/rule/UiThreadTestRule#apply(org.junit.runners.model.Statement, org.junit.runner.Description))`(Statement base, Description description) `                                                                                |
| ` void`      | ` `[runOnUiThread](https://developer.android.com/reference/android/support/test/rule/UiThreadTestRule#runOnUiThread(java.lang.Runnable))`(`[Runnable](https://developer.android.com/reference/java/lang/Runnable.html)` runnable) ` Helper method for running part of a method on the UI thread. |

|                                                                                         ### Protected methods                                                                                          ||
|------------|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| ` boolean` | ` `[shouldRunOnUiThread](https://developer.android.com/reference/android/support/test/rule/UiThreadTestRule#shouldRunOnUiThread(org.junit.runner.Description))`(Description description) ` |

| ### Inherited methods |
|-----------------------|---|
| [![](https://developer.android.com/static/assets/images/styles/disclosure_down.png)](https://developer.android.com/reference/android/support/test/rule/UiThreadTestRule#)From class` `[java.lang.Object](https://developer.android.com/reference/java/lang/Object.html)` ` |-------------------------------------------------------------------------------------|--------------------------------------------------------------------------------------------| | ` `[Object](https://developer.android.com/reference/java/lang/Object.html)          | ` clone() `                                                                                | | ` boolean`                                                                          | ` equals(`[Object](https://developer.android.com/reference/java/lang/Object.html)` arg0) ` | | ` void`                                                                             | ` finalize() `                                                                             | | ` final `[Class](https://developer.android.com/reference/java/lang/Class.html)`<?>` | ` getClass() `                                                                             | | ` int`                                                                              | ` hashCode() `                                                                             | | ` final void`                                                                       | ` notify() `                                                                               | | ` final void`                                                                       | ` notifyAll() `                                                                            | | ` `[String](https://developer.android.com/reference/java/lang/String.html)          | ` toString() `                                                                             | | ` final void`                                                                       | ` wait(long arg0, int arg1) `                                                              | | ` final void`                                                                       | ` wait(long arg0) `                                                                        | | ` final void`                                                                       | ` wait() `                                                                                 | ||
| [![](https://developer.android.com/static/assets/images/styles/disclosure_down.png)](https://developer.android.com/reference/android/support/test/rule/UiThreadTestRule#)From interface` org.junit.rules.TestRule ` |-----------------------|---------------------------------------------| | ` abstract Statement` | ` apply(Statement arg0, Description arg1) ` | ||

## Public constructors

### UiThreadTestRule

```
UiThreadTestRule ()
```

<br />

<br />

## Public methods

### apply

```
Statement apply (Statement base, 
                Description description)
```

<br />

<br />

|             Parameters              ||
|---------------|----------------------|
| `base`        | `Statement` <br />   |
| `description` | `Description` <br /> |

|       Returns       ||
|-------------|--------|
| `Statement` | <br /> |

### runOnUiThread

```
void runOnUiThread (Runnable runnable)
```

Helper method for running part of a method on the UI thread.

Note: In most cases it is simpler to annotate the test method with[UiThreadTest](https://developer.android.com/reference/android/support/test/annotation/UiThreadTest).

Use this method if you need to switch in and out of the UI thread within your method.

<br />

|                                                                       Parameters                                                                        ||
|------------|---------------------------------------------------------------------------------------------------------------------------------------------|
| `runnable` | `Runnable`: runnable containing test code in the[run()](https://developer.android.com/reference/java/lang/Runnable.html#run())method <br /> |

|                                      Throws                                      ||
|-------------------------------------------------------------------------------|---|
| [Throwable](https://developer.android.com/reference/java/lang/Throwable.html) |   |

**See also:**

- [UiThreadTest](https://developer.android.com/reference/android/support/test/annotation/UiThreadTest)

## Protected methods

### shouldRunOnUiThread

```
boolean shouldRunOnUiThread (Description description)
```

<br />

<br />

|             Parameters              ||
|---------------|----------------------|
| `description` | `Description` <br /> |

|      Returns      ||
|-----------|--------|
| `boolean` | <br /> |

-

  ## Classes

  - [ActivityTestRule](https://developer.android.com/reference/android/support/test/rule/ActivityTestRule)
  - [DisableOnAndroidDebug](https://developer.android.com/reference/android/support/test/rule/DisableOnAndroidDebug)
  - [GrantPermissionRule](https://developer.android.com/reference/android/support/test/rule/GrantPermissionRule)
  - [ServiceTestRule](https://developer.android.com/reference/android/support/test/rule/ServiceTestRule)
  - [UiThreadTestRule](https://developer.android.com/reference/android/support/test/rule/UiThreadTestRule)