# 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)

*** ** * ** ***


```
class UiThreadTestRule : TestRule
```

<br />

*** ** * ** ***

> [!CAUTION]
> **This class is deprecated.**   
>
> use `https://developer.android.com/reference/kotlin/androidx/test/annotation/UiThreadTest` directly without this rule. `https://developer.android.com/reference/kotlin/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/kotlin/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/kotlin/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/kotlin/androidx/test/rule/UiThreadTestRule#UiThreadTestRule())()` **This function is deprecated.** Deprecated in Java |

| ### Public functions |
|---|---|
| `https://junit.org/junit4/javadoc/latest/org/junit/runners/model/Statement.html!` | `[apply](https://developer.android.com/reference/kotlin/androidx/test/rule/UiThreadTestRule#apply(org.junit.runners.model.Statement,org.junit.runner.Description))(base: https://junit.org/junit4/javadoc/latest/org/junit/runners/model/Statement.html!, description: https://junit.org/junit4/javadoc/latest/org/junit/runner/Description.html!)` **This function is deprecated.** Deprecated in Java |
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | `[runOnUiThread](https://developer.android.com/reference/kotlin/androidx/test/rule/UiThreadTestRule#runOnUiThread(java.lang.Runnable))(runnable: https://developer.android.com/reference/java/lang/Runnable.html!)` **This function is deprecated.** Deprecated in Java |

| ### Protected functions |
|---|---|
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-boolean/index.html` | `[shouldRunOnUiThread](https://developer.android.com/reference/kotlin/androidx/test/rule/UiThreadTestRule#shouldRunOnUiThread(org.junit.runner.Description))(description: https://junit.org/junit4/javadoc/latest/org/junit/runner/Description.html!)` **This function is deprecated.** Deprecated in Java |

## Public constructors

### UiThreadTestRule

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

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

## Public functions

### apply

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

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

### runOnUiThread

```
fun [runOnUiThread](https://developer.android.com/reference/kotlin/androidx/test/rule/UiThreadTestRule#runOnUiThread(java.lang.Runnable))(runnable: Runnable!): Unit
```

> [!CAUTION]
> **This function 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/kotlin/androidx/test/annotation/UiThreadTest`.

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

| Parameters |
|---|---|
| `runnable: https://developer.android.com/reference/java/lang/Runnable.html!` | 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/kotlin/androidx/test/annotation/UiThreadTest` |   |

## Protected functions

### shouldRunOnUiThread

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

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