# 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-beta02](https://developer.android.com/jetpack/androidx/releases/xr-runtime#1.0.0-beta02)

*** ** * ** ***


```
public final class SessionTestRule extends ExternalResource
```

<br />

|---|---|---|
| [java.lang.Object](https://developer.android.com/reference/java/lang/Object.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/androidx/xr/runtime/testing/SessionTestRule) |

*** ** * ** ***

A JUnit TestRule for controlling the results of `https://developer.android.com/reference/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:

```
@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/androidx/xr/runtime/testing/SessionTestRule#SessionTestRule()()` |

| ### Public methods |
|---|---|
| `final https://developer.android.com/reference/androidx/xr/runtime/SessionConfigureResult` | `https://developer.android.com/reference/androidx/xr/runtime/testing/SessionTestRule#getConfigureResult()()` The result to be returned when `https://developer.android.com/reference/androidx/xr/runtime/Session#configure(androidx.xr.runtime.Config)` is called. |
| `final https://developer.android.com/reference/androidx/xr/runtime/SessionCreateResult` | `https://developer.android.com/reference/androidx/xr/runtime/testing/SessionTestRule#getCreateResult()()` The result to be returned when `https://developer.android.com/reference/androidx/xr/runtime/Session#create(android.content.Context,kotlin.coroutines.CoroutineContext,androidx.lifecycle.LifecycleOwner)` is called. |
| `final void` | `https://developer.android.com/reference/androidx/xr/runtime/testing/SessionTestRule#setConfigureResult(androidx.xr.runtime.SessionConfigureResult)(https://developer.android.com/reference/androidx/xr/runtime/SessionConfigureResult value)` The result to be returned when `https://developer.android.com/reference/androidx/xr/runtime/Session#configure(androidx.xr.runtime.Config)` is called. |
| `final void` | `https://developer.android.com/reference/androidx/xr/runtime/testing/SessionTestRule#setCreateResult(androidx.xr.runtime.SessionCreateResult)(https://developer.android.com/reference/androidx/xr/runtime/SessionCreateResult value)` The result to be returned when `https://developer.android.com/reference/androidx/xr/runtime/Session#create(android.content.Context,kotlin.coroutines.CoroutineContext,androidx.lifecycle.LifecycleOwner)` is called. |

| ### Protected methods |
|---|---|
| `void` | `https://developer.android.com/reference/androidx/xr/runtime/testing/SessionTestRule#after()()` |
| `void` | `https://developer.android.com/reference/androidx/xr/runtime/testing/SessionTestRule#before()()` |

### Inherited methods

From [org.junit.rules.ExternalResource](https://junit.org/junit4/javadoc/latest/org/junit/rules/ExternalResource.html) `@https://developer.android.com/reference/androidx/annotation/NonNull 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-(@https://developer.android.com/reference/androidx/annotation/NonNull https://junit.org/junit4/javadoc/latest/org/junit/runners/model/Statement.html base, @https://developer.android.com/reference/androidx/annotation/NonNull https://junit.org/junit4/javadoc/latest/org/junit/runner/Description.html description)`

## Public constructors

### SessionTestRule

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

```
public SessionTestRule()
```

## Public methods

### getConfigureResult

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

```
public final SessionConfigureResult getConfigureResult()
```

The result to be returned when `https://developer.android.com/reference/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/androidx/xr/runtime/SessionConfigureSuccess`, other results are possible, depending on the underlying selected runtime implementation.

### getCreateResult

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

```
public final SessionCreateResult getCreateResult()
```

The result to be returned when `https://developer.android.com/reference/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/androidx/xr/runtime/SessionCreateSuccess`, other results are possible, depending on the underlying selected runtime implementation.

### setConfigureResult

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

```
public final void setConfigureResult(SessionConfigureResult value)
```

The result to be returned when `https://developer.android.com/reference/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/androidx/xr/runtime/SessionConfigureSuccess`, other results are possible, depending on the underlying selected runtime implementation.

### setCreateResult

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

```
public final void setCreateResult(SessionCreateResult value)
```

The result to be returned when `https://developer.android.com/reference/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/androidx/xr/runtime/SessionCreateSuccess`, other results are possible, depending on the underlying selected runtime implementation.

## Protected methods

### after

```
protected void after()
```

### before

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

```
protected void before()
```