# UiThreadTestRule

Artifact: [androidx.test:rules](https://developer.android.com/jetpack/androidx/releases/test) [View Source](https://cs.android.com/search?q=file:androidx/test/rule/UiThreadTestRule.java+class:androidx.test.rule.UiThreadTestRule)

*** ** * ** ***


```
public class UiThreadTestRule implements TestRule
```

<br />

*** ** * ** ***

> [!CAUTION]
> **This class is deprecated.**   
>
> use `https://developer.android.com/reference/androidx/test/annotation/UiThreadTest` directly without this rule. `https://developer.android.com/reference/androidx/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 `https://developer.android.com/reference/androidx/test/annotation/UiThreadTest` to execute on the application's main thread (or UI thread).

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

| See also |
|---|---|
| `https://developer.android.com/reference/androidx/test/annotation/UiThreadTest` | if you need to switch in and out of the UI thread within your method. |

## Summary

| ### Public constructors |
|---|
| `[UiThreadTestRule](https://developer.android.com/reference/androidx/test/rule/UiThreadTestRule#UiThreadTestRule())()` **This method is deprecated.** Deprecated in Java |

| ### Public methods |
|---|---|
| `https://junit.org/junit4/javadoc/latest/org/junit/runners/model/Statement.html` | `[apply](https://developer.android.com/reference/androidx/test/rule/UiThreadTestRule#apply(org.junit.runners.model.Statement,org.junit.runner.Description))(https://junit.org/junit4/javadoc/latest/org/junit/runners/model/Statement.html base, https://junit.org/junit4/javadoc/latest/org/junit/runner/Description.html description)` **This method is deprecated.** Deprecated in Java |
| `void` | `[runOnUiThread](https://developer.android.com/reference/androidx/test/rule/UiThreadTestRule#runOnUiThread(java.lang.Runnable))(https://developer.android.com/reference/java/lang/Runnable.html runnable)` **This method is deprecated.** Deprecated in Java |

| ### Protected methods |
|---|---|
| `boolean` | `[shouldRunOnUiThread](https://developer.android.com/reference/androidx/test/rule/UiThreadTestRule#shouldRunOnUiThread(org.junit.runner.Description))(https://junit.org/junit4/javadoc/latest/org/junit/runner/Description.html description)` **This method is deprecated.** Deprecated in Java |

## Public constructors

### UiThreadTestRule

```
public [UiThreadTestRule](https://developer.android.com/reference/androidx/test/rule/UiThreadTestRule#UiThreadTestRule())()
```

> [!CAUTION]
> **This method is deprecated.**   
> Deprecated in Java

## Public methods

### apply

```
public Statement [apply](https://developer.android.com/reference/androidx/test/rule/UiThreadTestRule#apply(org.junit.runners.model.Statement,org.junit.runner.Description))(Statement base, Description description)
```

> [!CAUTION]
> **This method is deprecated.**   
> Deprecated in Java

### runOnUiThread

```
public void [runOnUiThread](https://developer.android.com/reference/androidx/test/rule/UiThreadTestRule#runOnUiThread(java.lang.Runnable))(Runnable runnable)
```

> [!CAUTION]
> **This method is deprecated.**   
> Deprecated in Java

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 `https://developer.android.com/reference/androidx/test/annotation/UiThreadTest`.

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

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

| Throws |
|---|---|
| `https://developer.android.com/reference/java/lang/Throwable.html` |   |

| See also |
|---|---|
| `https://developer.android.com/reference/androidx/test/annotation/UiThreadTest` |   |

## Protected methods

### shouldRunOnUiThread

```
protected boolean [shouldRunOnUiThread](https://developer.android.com/reference/androidx/test/rule/UiThreadTestRule#shouldRunOnUiThread(org.junit.runner.Description))(Description description)
```

> [!CAUTION]
> **This method is deprecated.**   
> Deprecated in Java