This class is deprecated.
use UiThreadTest directly without this rule. 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 to execute on the
application's main thread (or UI thread).
Note, methods annotated with Before and After will also
be executed on the UI thread.
Content and code samples on this page are subject to the licenses described in the Content License. Java and OpenJDK are trademarks or registered trademarks of Oracle and/or its affiliates.
Last updated 2025-02-10 UTC.
[null,null,["Last updated 2025-02-10 UTC."],[],[],null,["# UiThreadTestRule\n\nSummary: [Ctors](#pubctors) \\| [Methods](#pubmethods) \\| [Protected Methods](#promethods) \\| [Inherited Methods](#inhmethods) \\| [\\[Expand All\\]](#) \n\nUiThreadTestRule\n================\n\n| This package is part of the Android [support library](/topic/libraries/support-library) which is no longer maintained. The support library has been superseded by [AndroidX](/jetpack/androidx) which is part of [Jetpack](/jetpack). We recommend using the AndroidX libraries in all new projects. You should also consider [migrating](/jetpack/androidx/migrate) existing projects to AndroidX. To find the AndroidX class that maps to this deprecated class, see the AndroidX support library [class\n| mappings](/jetpack/androidx/migrate/class-mappings).\n\n\n`\npublic\n\n\nclass\nUiThreadTestRule\n`\n\n\n`\n\nextends `[Object](https://developer.android.com/reference/java/lang/Object.html)`\n\n\n`\n\n`\n\n\nimplements\n\nTestRule\n\n\n`\n\n|---|--------------------------------------------|\n| [java.lang.Object](https://developer.android.com/reference/java/lang/Object.html) ||\n| ↳ | android.support.test.rule.UiThreadTestRule |\n\n\u003cbr /\u003e\n\n*** ** * ** ***\n\n\n**This class is deprecated.** \n\nuse [UiThreadTest](/reference/android/support/test/annotation/UiThreadTest) directly without this rule. [UiThreadTest](/reference/android/support/test/annotation/UiThreadTest) is now\nsupported as part of the core Android test runner to provide the ability to run methods\nannotated with `@Before` and `@After` on the UI thread regardless of\nwhat `@Test` is annotated with.\n\nThis rule allows the test method annotated with [UiThreadTest](/reference/android/support/test/annotation/UiThreadTest) to execute on the\napplication's main thread (or UI thread).\n\nNote, 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\nbe executed on the UI thread. \n**See also:**\n\n- [if you need to switch in and out of the UI\n thread within your method.](/reference/android/support/test/annotation/UiThreadTest)\n\nSummary\n-------\n\n| ### Public constructors ||\n|------------------------------------------------------------------------------------------------------|---|\n| ` `[UiThreadTestRule](/reference/android/support/test/rule/UiThreadTestRule#UiThreadTestRule())`() ` |\n\n| ### Public methods ||\n|--------------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| ` Statement` | ` `[apply](/reference/android/support/test/rule/UiThreadTestRule#apply(org.junit.runners.model.Statement, org.junit.runner.Description))`(Statement base, Description description) ` |\n| ` void` | ` `[runOnUiThread](/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. |\n\n| ### Protected methods ||\n|------------|---------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| ` boolean` | ` `[shouldRunOnUiThread](/reference/android/support/test/rule/UiThreadTestRule#shouldRunOnUiThread(org.junit.runner.Description))`(Description description) ` |\n\n| ### Inherited methods |\n|-----------------------|---|\n| 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)`\u003c?\u003e` | ` 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() ` | ||\n| From interface ` org.junit.rules.TestRule ` |-----------------------|---------------------------------------------| | ` abstract Statement` | ` apply(Statement arg0, Description arg1) ` | ||\n\nPublic constructors\n-------------------\n\n### UiThreadTestRule\n\n```\nUiThreadTestRule ()\n```\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\nPublic methods\n--------------\n\n### apply\n\n```\nStatement apply (Statement base, \n Description description)\n```\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Parameters ||\n|---------------|----------------------|\n| `base` | `Statement` \u003cbr /\u003e |\n| `description` | `Description` \u003cbr /\u003e |\n\n| Returns ||\n|-------------|--------|\n| `Statement` | \u003cbr /\u003e |\n\n### runOnUiThread\n\n```\nvoid runOnUiThread (Runnable runnable)\n```\n\nHelper method for running part of a method on the UI thread.\n\nNote: In most cases it is simpler to annotate the test method with [UiThreadTest](/reference/android/support/test/annotation/UiThreadTest).\n\nUse this method if you need to switch in and out of the UI thread within your method.\n\n\u003cbr /\u003e\n\n| Parameters ||\n|------------|-----------------------------------------------------------------------------------------------------------------------------------------------|\n| `runnable` | `Runnable`: runnable containing test code in the [run()](https://developer.android.com/reference/java/lang/Runnable.html#run()) method \u003cbr /\u003e |\n\n| Throws ||\n|-------------------------------------------------------------------------------|---|\n| [Throwable](https://developer.android.com/reference/java/lang/Throwable.html) | |\n\n**See also:**\n\n- [UiThreadTest](/reference/android/support/test/annotation/UiThreadTest)\n\nProtected methods\n-----------------\n\n### shouldRunOnUiThread\n\n```\nboolean shouldRunOnUiThread (Description description)\n```\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Parameters ||\n|---------------|----------------------|\n| `description` | `Description` \u003cbr /\u003e |\n\n| Returns ||\n|-----------|--------|\n| `boolean` | \u003cbr /\u003e |\n\n-\n\n Classes\n -------\n\n - [ActivityTestRule](/reference/android/support/test/rule/ActivityTestRule)\n - [DisableOnAndroidDebug](/reference/android/support/test/rule/DisableOnAndroidDebug)\n - [GrantPermissionRule](/reference/android/support/test/rule/GrantPermissionRule)\n - [ServiceTestRule](/reference/android/support/test/rule/ServiceTestRule)\n - [UiThreadTestRule](/reference/android/support/test/rule/UiThreadTestRule)"]]