Stay organized with collections
Save and categorize content based on your preferences.
@Target(value = [ElementType.METHOD, ElementType.TYPE])
@Retention(value = RetentionPolicy.RUNTIME)
public annotation UiThreadTest
Classes and methods annotated with this annotation will be executed on the application's UI thread (or main thread).
This annotation will only take effect for org.junit.Test
, org.junit.Before
or org.junit.After
methods.
Use runOnMainSync
if you need to switch in and out of the UI thread within your method.
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-07-17 UTC.
[null,null,["Last updated 2025-07-17 UTC."],[],[],null,["# UiThreadTest\n============\n\nArtifact: [androidx.test:rules](/jetpack/androidx/releases/test) \n[View Source](https://cs.android.com/search?q=file:androidx/test/annotation/UiThreadTest.java+class:androidx.test.annotation.UiThreadTest)\n\n*** ** * ** ***\n\n[Kotlin](/reference/kotlin/androidx/test/annotation/UiThreadTest \"View this page in Kotlin\") \\|Java\n\n\n```\n@Target(value = [ElementType.METHOD, ElementType.TYPE])\n@Retention(value = RetentionPolicy.RUNTIME)\npublic annotation UiThreadTest\n```\n\n\u003cbr /\u003e\n\n*** ** * ** ***\n\nClasses and methods annotated with this annotation will be executed on the application's UI thread (or main thread).\n\nThis annotation will only take effect for [org.junit.Test](https://junit.org/junit4/javadoc/4.12/org/junit/Test.html), [org.junit.Before](https://junit.org/junit4/javadoc/4.12/org/junit/Before.html) or [org.junit.After](https://junit.org/junit4/javadoc/4.12/org/junit/After.html) methods.\n\nUse [runOnMainSync](https://developer.android.com/reference/android/app/Instrumentation.html#runOnMainSync(java.lang.Runnable)) if you need to switch in and out of the UI thread within your method."]]