# SessionTestRule

Artifact: [androidx.xr.runtime:runtime-testing](https://developer.android.com/jetpack/androidx/releases/xr-runtime) [View Source](https://cs.android.com/search?q=file:androidx/xr/runtime/testing/SessionTestRule.kt+class:androidx.xr.runtime.testing.SessionTestRule) Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/xr-runtime#1.0.0)

*** ** * ** ***


```
class SessionTestRule : ExternalResource
```

<br />

|---|---|---|
| [kotlin.Any](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-any/index.html) |||
| ↳ | [org.junit.rules.ExternalResource](https://junit.org/junit4/javadoc/latest/org/junit/rules/ExternalResource.html) ||
|   | ↳ | [androidx.xr.runtime.testing.SessionTestRule](https://developer.android.com/reference/kotlin/androidx/xr/runtime/testing/SessionTestRule) |

*** ** * ** ***

A JUnit TestRule for controlling the results of `https://developer.android.com/reference/kotlin/androidx/xr/runtime/Session` operations within tests. This rule intercepts calls to create and configure sessions, allowing tests to inject specific success or failure results.

To use this rule, include it in your test class:

```kotlin
@RunWith(JUnit4::class)
class MySessionTest {
    @get:Rule
    val sessionTestRule = SessionTestRule()

    @Test
    fun testSessionCreationFails() = runTest {
        sessionTestRule.createResult = SessionCreateResult.Failure(IllegalArgumentException("Test"))
        // ... code that calls Session.create() ...
    }

    @Test
    fun testSessionConfigurationSuccess() = runTest {
        sessionTestRule.configureResult = SessionConfigureResult.Success
        // ... code that calls Session.configure() ...
    }
}
```

## Summary

| ### Public constructors |
|---|
| `https://developer.android.com/reference/kotlin/androidx/xr/runtime/testing/SessionTestRule#SessionTestRule()()` |

| ### Protected functions |
|---|---|
| `open https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | `https://developer.android.com/reference/kotlin/androidx/xr/runtime/testing/SessionTestRule#after()()` |
| `open https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | `https://developer.android.com/reference/kotlin/androidx/xr/runtime/testing/SessionTestRule#before()()` |

| ### Public properties |
|---|---|
| `https://developer.android.com/reference/kotlin/androidx/xr/runtime/SessionConfigureResult?` | `https://developer.android.com/reference/kotlin/androidx/xr/runtime/testing/SessionTestRule#configureResult()` The result to be returned when `https://developer.android.com/reference/kotlin/androidx/xr/runtime/Session#configure(androidx.xr.runtime.Config)` is called. |
| `https://developer.android.com/reference/kotlin/androidx/xr/runtime/SessionCreateResult?` | `https://developer.android.com/reference/kotlin/androidx/xr/runtime/testing/SessionTestRule#createResult()` The result to be returned when `https://developer.android.com/reference/kotlin/androidx/xr/runtime/Session#create(android.content.Context,kotlin.coroutines.CoroutineContext,androidx.lifecycle.LifecycleOwner)` is called. |

### Inherited functions

From [org.junit.rules.ExternalResource](https://junit.org/junit4/javadoc/latest/org/junit/rules/ExternalResource.html) `https://junit.org/junit4/javadoc/latest/org/junit/runners/model/Statement.html` `https://junit.org/junit4/javadoc/latest/org/junit/rules/ExternalResource.html#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)`

## Public constructors

### SessionTestRule

Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/xr-runtime#1.0.0)

```
SessionTestRule()
```

## Protected functions

### after

```
protected open fun after(): Unit
```

### before

Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/xr-runtime#1.0.0)

```
protected open fun before(): Unit
```

## Public properties

### configureResult

Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/xr-runtime#1.0.0)

```
var configureResult: SessionConfigureResult?
```

The result to be returned when `https://developer.android.com/reference/kotlin/androidx/xr/runtime/Session#configure(androidx.xr.runtime.Config)` is called. If null, the default behavior of the runtime will be used. While this will likely be `https://developer.android.com/reference/kotlin/androidx/xr/runtime/SessionConfigureSuccess`, other results are possible, depending on the underlying selected runtime implementation.

### createResult

Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/xr-runtime#1.0.0)

```
var createResult: SessionCreateResult?
```

The result to be returned when `https://developer.android.com/reference/kotlin/androidx/xr/runtime/Session#create(android.content.Context,kotlin.coroutines.CoroutineContext,androidx.lifecycle.LifecycleOwner)` is called. If null, the default behavior of the runtime will be used. While this will likely be `https://developer.android.com/reference/kotlin/androidx/xr/runtime/SessionCreateSuccess`, other results are possible, depending on the underlying selected runtime implementation.