# androidx.compose.ui.test.junit4

Android/JVM

## Interfaces

|---|---|---|
| `https://developer.android.com/reference/kotlin/androidx/compose/ui/test/junit4/ComposeContentTestRule` | A `https://developer.android.com/reference/kotlin/androidx/compose/ui/test/junit4/ComposeTestRule` that allows you to set content without the necessity to provide a host for the content. | android |
| `https://developer.android.com/reference/kotlin/androidx/compose/ui/test/junit4/ComposeTestRule` | A `https://junit.org/junit4/javadoc/latest/org/junit/rules/TestRule.html` that allows you to test and control composables, either in isolation or in applications. | android |

## Classes

|---|---|---|
| `https://developer.android.com/reference/kotlin/androidx/compose/ui/test/junit4/AndroidComposeTestRule` |   | android |
| `https://developer.android.com/reference/kotlin/androidx/compose/ui/test/junit4/AndroidComposeTestRule.AndroidComposeStatement` | **This class is deprecated.** Do not instantiate this Statement, use AndroidComposeTestRule instead | android |
| `https://developer.android.com/reference/kotlin/androidx/compose/ui/test/junit4/StateRestorationTester` | Helps to test the state restoration for your Composable component. | android |

## Top-level functions summary

|---|---|---|
| `inline https://developer.android.com/reference/kotlin/androidx/compose/ui/test/junit4/AndroidComposeTestRule<https://developer.android.com/reference/kotlin/androidx/test/ext/junit/rules/ActivityScenarioRule<A>, A>` | `<A : https://developer.android.com/reference/kotlin/androidx/activity/ComponentActivity> [createAndroidComposeRule](https://developer.android.com/reference/kotlin/androidx/compose/ui/test/junit4/package-summary#createAndroidComposeRule(kotlin.coroutines.CoroutineContext))( effectContext: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.coroutines/-coroutine-context/index.html )` **This function is deprecated.** Use \`androidx.compose.ui.test.junit4.v2.createAndroidComposeRule\` instead. | android |
| `https://developer.android.com/reference/kotlin/androidx/compose/ui/test/junit4/AndroidComposeTestRule<https://developer.android.com/reference/kotlin/androidx/test/ext/junit/rules/ActivityScenarioRule<A>, A>` | `<A : https://developer.android.com/reference/kotlin/androidx/activity/ComponentActivity> [createAndroidComposeRule](https://developer.android.com/reference/kotlin/androidx/compose/ui/test/junit4/package-summary#createAndroidComposeRule(java.lang.Class,kotlin.coroutines.CoroutineContext))( activityClass: https://developer.android.com/reference/java/lang/Class.html<A>, effectContext: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.coroutines/-coroutine-context/index.html )` **This function is deprecated.** Use \`androidx.compose.ui.test.junit4.v2.createAndroidComposeRule\` instead. | android |
| `https://developer.android.com/reference/kotlin/androidx/compose/ui/test/junit4/ComposeContentTestRule` | `[createComposeRule](https://developer.android.com/reference/kotlin/androidx/compose/ui/test/junit4/package-summary#createComposeRule(kotlin.coroutines.CoroutineContext))(effectContext: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.coroutines/-coroutine-context/index.html)` **This function is deprecated.** Use \`androidx.compose.ui.test.junit4.v2.createComposeRule\` instead. | android |
| `https://developer.android.com/reference/kotlin/androidx/compose/ui/test/junit4/ComposeTestRule` | `[createEmptyComposeRule](https://developer.android.com/reference/kotlin/androidx/compose/ui/test/junit4/package-summary#createEmptyComposeRule(kotlin.coroutines.CoroutineContext))(effectContext: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.coroutines/-coroutine-context/index.html)` **This function is deprecated.** Use \`androidx.compose.ui.test.junit4.v2.createEmptyComposeRule\` instead. | android |

## Extension functions summary

|---|---|---|
| `https://developer.android.com/reference/kotlin/androidx/compose/ui/test/SemanticsNodeInteractionsProvider` | `https://developer.android.com/reference/kotlin/androidx/compose/ui/test/junit4/ComposeTestRule.https://developer.android.com/reference/kotlin/androidx/compose/ui/test/junit4/package-summary#(androidx.compose.ui.test.junit4.ComposeTestRule).onRootWithViewInteraction(androidx.test.espresso.ViewInteraction)( interaction: https://developer.android.com/reference/kotlin/androidx/test/espresso/ViewInteraction )` Scopes the Compose interaction to the View hierarchy matched by the provided Espresso `https://developer.android.com/reference/kotlin/androidx/test/espresso/ViewInteraction`. | android |

## Top-level functions

### createAndroidComposeRule

android Artifact: [androidx.compose.ui:ui-test-junit4](https://developer.android.com/jetpack/androidx/releases/compose-ui) [View Source](https://cs.android.com/search?q=file:androidx/compose/ui/test/junit4/AndroidComposeTestRule.android.kt+function:createAndroidComposeRule)

```
inline fun <A : ComponentActivity> [createAndroidComposeRule](https://developer.android.com/reference/kotlin/androidx/compose/ui/test/junit4/package-summary#createAndroidComposeRule(kotlin.coroutines.CoroutineContext))(
    effectContext: CoroutineContext = EmptyCoroutineContext
): AndroidComposeTestRule<ActivityScenarioRule<A>, A>
```

> [!CAUTION]
> **This function is deprecated.**   
> Use \`androidx.compose.ui.test.junit4.v2.createAndroidComposeRule\` instead. The v2 APIs use StandardTestDispatcher instead of UnconfinedTestDispatcher, which aligns with standard coroutine behavior by queuing tasks rather than executing them immediately. Tests relying on immediate execution may require explicit synchronization. Please refer to the migration guide for more details.

Factory method to provide android specific implementation of `https://developer.android.com/reference/kotlin/androidx/compose/ui/test/junit4/package-summary#createComposeRule(kotlin.coroutines.CoroutineContext)`, for a given activity class type `https://developer.android.com/reference/kotlin/androidx/compose/ui/test/junit4/package-summary#createAndroidComposeRule(kotlin.coroutines.CoroutineContext)`.

This method is useful for tests that require a custom Activity. This is usually the case for tests where the compose content is set by that Activity, instead of via the test rule's `https://developer.android.com/reference/kotlin/androidx/compose/ui/test/junit4/ComposeContentTestRule#setContent(kotlin.Function0)`. Make sure that you add the provided activity into your app's manifest file (usually in main/AndroidManifest.xml).

This creates a test rule that is using `https://developer.android.com/reference/kotlin/androidx/test/ext/junit/rules/ActivityScenarioRule` as the activity launcher. If you would like to use a different one you can create `https://developer.android.com/reference/kotlin/androidx/compose/ui/test/junit4/AndroidComposeTestRule` directly and supply it with your own launcher.

If your test doesn't require a specific Activity, use `https://developer.android.com/reference/kotlin/androidx/compose/ui/test/junit4/package-summary#createComposeRule(kotlin.coroutines.CoroutineContext)` instead.

| Parameters |
|---|---|
| `effectContext: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.coroutines/-coroutine-context/index.html = EmptyCoroutineContext` | The `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.coroutines/-coroutine-context/index.html` used to run the composition. The context for `LaunchedEffect`s and `rememberCoroutineScope` will be derived from this context. If this context contains a `https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-test/kotlinx.coroutines.test/-test-dispatcher/index.html` or `https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-test/kotlinx.coroutines.test/-test-coroutine-scheduler/index.html` (in that order), it will be used for composition and the `https://developer.android.com/reference/kotlin/androidx/compose/ui/test/MainTestClock`. |

### createAndroidComposeRule

android Artifact: [androidx.compose.ui:ui-test-junit4](https://developer.android.com/jetpack/androidx/releases/compose-ui) [View Source](https://cs.android.com/search?q=file:androidx/compose/ui/test/junit4/AndroidComposeTestRule.android.kt+function:createAndroidComposeRule)

```
fun <A : ComponentActivity> [createAndroidComposeRule](https://developer.android.com/reference/kotlin/androidx/compose/ui/test/junit4/package-summary#createAndroidComposeRule(java.lang.Class,kotlin.coroutines.CoroutineContext))(
    activityClass: Class<A>,
    effectContext: CoroutineContext = EmptyCoroutineContext
): AndroidComposeTestRule<ActivityScenarioRule<A>, A>
```

> [!CAUTION]
> **This function is deprecated.**   
> Use \`androidx.compose.ui.test.junit4.v2.createAndroidComposeRule\` instead. The v2 APIs use StandardTestDispatcher instead of UnconfinedTestDispatcher, which aligns with standard coroutine behavior by queuing tasks rather than executing them immediately. Tests relying on immediate execution may require explicit synchronization. Please refer to the migration guide for more details.

Factory method to provide android specific implementation of `https://developer.android.com/reference/kotlin/androidx/compose/ui/test/junit4/package-summary#createComposeRule(kotlin.coroutines.CoroutineContext)`, for a given `https://developer.android.com/reference/kotlin/androidx/compose/ui/test/junit4/package-summary#createAndroidComposeRule(java.lang.Class,kotlin.coroutines.CoroutineContext)`.

This method is useful for tests that require a custom Activity. This is usually the case for tests where the compose content is set by that Activity, instead of via the test rule's `https://developer.android.com/reference/kotlin/androidx/compose/ui/test/junit4/ComposeContentTestRule#setContent(kotlin.Function0)`. Make sure that you add the provided activity into your app's manifest file (usually in main/AndroidManifest.xml).

This creates a test rule that is using `https://developer.android.com/reference/kotlin/androidx/test/ext/junit/rules/ActivityScenarioRule` as the activity launcher. If you would like to use a different one you can create `https://developer.android.com/reference/kotlin/androidx/compose/ui/test/junit4/AndroidComposeTestRule` directly and supply it with your own launcher.

If your test doesn't require a specific Activity, use `https://developer.android.com/reference/kotlin/androidx/compose/ui/test/junit4/package-summary#createComposeRule(kotlin.coroutines.CoroutineContext)` instead.

| Parameters |
|---|---|
| `activityClass: https://developer.android.com/reference/java/lang/Class.html<A>` | The activity type to use in the activity scenario |
| `effectContext: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.coroutines/-coroutine-context/index.html = EmptyCoroutineContext` | The `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.coroutines/-coroutine-context/index.html` used to run the composition. The context for `LaunchedEffect`s and `rememberCoroutineScope` will be derived from this context. If this context contains a `https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-test/kotlinx.coroutines.test/-test-dispatcher/index.html` or `https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-test/kotlinx.coroutines.test/-test-coroutine-scheduler/index.html` (in that order), it will be used for composition and the `https://developer.android.com/reference/kotlin/androidx/compose/ui/test/MainTestClock`. |

### createComposeRule

android [View Source](https://cs.android.com/search?q=file:androidx/compose/ui/test/junit4/+function:createComposeRule)

```
fun [createComposeRule](https://developer.android.com/reference/kotlin/androidx/compose/ui/test/junit4/package-summary#createComposeRule(kotlin.coroutines.CoroutineContext))(effectContext: CoroutineContext): ComposeContentTestRule
```

> [!CAUTION]
> **This function is deprecated.**   
> Use \`androidx.compose.ui.test.junit4.v2.createComposeRule\` instead. The v2 APIs use StandardTestDispatcher instead of UnconfinedTestDispatcher, which aligns with standard coroutine behavior by queuing tasks rather than executing them immediately. Tests relying on immediate execution may require explicit synchronization. Please refer to the migration guide for more details.

Factory method to provide an implementation of `https://developer.android.com/reference/kotlin/androidx/compose/ui/test/junit4/ComposeContentTestRule`.

This method is useful for tests in compose libraries where it is irrelevant where the compose content is hosted (e.g. an Activity on Android). Such tests typically set compose content themselves via `https://developer.android.com/reference/kotlin/androidx/compose/ui/test/junit4/ComposeContentTestRule#setContent(kotlin.Function0)` and only instrument and assert that content.

For Android this will use the default Activity (android.app.Activity). You need to add a reference to this activity into the manifest file of the corresponding tests (usually in androidTest/AndroidManifest.xml). If your Android test requires a specific Activity to be launched, see `https://developer.android.com/reference/kotlin/androidx/compose/ui/test/junit4/package-summary#createAndroidComposeRule(kotlin.coroutines.CoroutineContext)`.

| Parameters |
|---|---|
| `effectContext: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.coroutines/-coroutine-context/index.html` | The `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.coroutines/-coroutine-context/index.html` used to run the composition. The context for `LaunchedEffect`s and `rememberCoroutineScope` will be derived from this context. If this context contains a `https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-test/kotlinx.coroutines.test/-test-dispatcher/index.html` or `https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-test/kotlinx.coroutines.test/-test-coroutine-scheduler/index.html` (in that order), it will be used for composition and the `https://developer.android.com/reference/kotlin/androidx/compose/ui/test/MainTestClock`. |

### createEmptyComposeRule

android Artifact: [androidx.compose.ui:ui-test-junit4](https://developer.android.com/jetpack/androidx/releases/compose-ui) [View Source](https://cs.android.com/search?q=file:androidx/compose/ui/test/junit4/AndroidComposeTestRule.android.kt+function:createEmptyComposeRule)

```
fun [createEmptyComposeRule](https://developer.android.com/reference/kotlin/androidx/compose/ui/test/junit4/package-summary#createEmptyComposeRule(kotlin.coroutines.CoroutineContext))(
    effectContext: CoroutineContext = EmptyCoroutineContext
): ComposeTestRule
```

> [!CAUTION]
> **This function is deprecated.**   
> Use \`androidx.compose.ui.test.junit4.v2.createEmptyComposeRule\` instead. The v2 APIs use StandardTestDispatcher instead of UnconfinedTestDispatcher, which aligns with standard coroutine behavior by queuing tasks rather than executing them immediately. Tests relying on immediate execution may require explicit synchronization. Please refer to the migration guide for more details.

Factory method to provide an implementation of `https://developer.android.com/reference/kotlin/androidx/compose/ui/test/junit4/ComposeTestRule` that doesn't create a compose host for you in which you can set content.

This method is useful for tests that need to create their own compose host during the test. The returned test rule will not create a host, and consequently does not provide a `setContent` method. To set content in tests using this rule, use the appropriate `setContent` methods from your compose host.

A typical use case on Android is when the test needs to launch an Activity (the compose host) after one or more dependencies have been injected.

| Parameters |
|---|---|
| `effectContext: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.coroutines/-coroutine-context/index.html = EmptyCoroutineContext` | The `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.coroutines/-coroutine-context/index.html` used to run the composition. The context for `LaunchedEffect`s and `rememberCoroutineScope` will be derived from this context. If this context contains a `https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-test/kotlinx.coroutines.test/-test-dispatcher/index.html` or `https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-test/kotlinx.coroutines.test/-test-coroutine-scheduler/index.html` (in that order), it will be used for composition and the `https://developer.android.com/reference/kotlin/androidx/compose/ui/test/MainTestClock`. |

## Extension functions

### ComposeTestRule.onRootWithViewInteraction

android Artifact: [androidx.compose.ui:ui-test-junit4](https://developer.android.com/jetpack/androidx/releases/compose-ui) [View Source](https://cs.android.com/search?q=file:androidx/compose/ui/test/junit4/ComposeTestRuleExt.android.kt+function:onRootWithViewInteraction)

```
fun ComposeTestRule.onRootWithViewInteraction(
    interaction: ViewInteraction
): SemanticsNodeInteractionsProvider
```

Scopes the Compose interaction to the View hierarchy matched by the provided Espresso `https://developer.android.com/reference/kotlin/androidx/test/espresso/ViewInteraction`.

It resolves the View from the Espresso `https://developer.android.com/reference/kotlin/androidx/compose/ui/test/junit4/package-summary#(androidx.compose.ui.test.junit4.ComposeTestRule).onRootWithViewInteraction(androidx.test.espresso.ViewInteraction)`, locates all Compose roots within that view hierarchy, and creates a new, scoped SemanticsNodeInteractionsProvider.

```kotlin
import androidx.compose.ui.test.junit4.onRootWithViewInteraction
import androidx.compose.ui.test.onNodeWithContentDescription
import androidx.compose.ui.test.performClick
import androidx.test.espresso.Espresso.onView
import androidx.test.espresso.matcher.ViewMatchers.withId

// Select the "Header" View container
val headerInteraction = onView(withId(header_id))

// Scope the Compose interaction to only the Header
composeTestRule
    .onRootWithViewInteraction(headerInteraction)
    .onNodeWithContentDescription("Settings")
    .performClick()
```

```kotlin
import androidx.compose.ui.test.assertIsDisplayed
import androidx.compose.ui.test.junit4.onRootWithViewInteraction
import androidx.compose.ui.test.onNodeWithTag
import androidx.compose.ui.test.performClick
import androidx.test.espresso.Espresso.onView
import androidx.test.espresso.matcher.ViewMatchers.hasDescendant
import androidx.test.espresso.matcher.ViewMatchers.withId
import androidx.test.espresso.matcher.ViewMatchers.withText

// Select the specific View row containing "Item #5"
val specificRowInteraction =
    onView(allOf(withId(recycler_item_root_id), hasDescendant(withText("Item #5"))))

// Scope interaction to that specific row View
composeTestRule
    .onRootWithViewInteraction(specificRowInteraction)
    .onNodeWithTag("fav_icon")
    .assertIsDisplayed()
    .performClick()
```

```kotlin
import androidx.compose.ui.test.assertIsEnabled
import androidx.compose.ui.test.junit4.onRootWithViewInteraction
import androidx.compose.ui.test.onNodeWithText
import androidx.test.espresso.Espresso.onView
import androidx.test.espresso.matcher.ViewMatchers.withId

// Select the container for the Detail Fragment
val detailContainerInteraction = onView(withId(detail_fragment_container_id))

// Assert that the submit button exists/is enabled only in the detail fragment
composeTestRule
    .onRootWithViewInteraction(detailContainerInteraction)
    .onNodeWithText("Submit")
    .assertIsEnabled()
```