# androidx.health.connect.client.testing.stubs

*** ** * ** ***

## Interfaces

|---|---|
| `https://developer.android.com/reference/kotlin/androidx/health/connect/client/testing/stubs/Stub` | Represents a stub mapping from a request `https://developer.android.com/reference/kotlin/androidx/health/connect/client/testing/stubs/Stub` to a response `https://developer.android.com/reference/kotlin/androidx/health/connect/client/testing/stubs/Stub`. |

## Classes

|---|---|
| `https://developer.android.com/reference/kotlin/androidx/health/connect/client/testing/stubs/MutableStub` | A `https://developer.android.com/reference/kotlin/androidx/health/connect/client/testing/stubs/Stub` whose defaultHandler and queue can be mutated at any point. |

## Top-level functions summary

|---|---|
| `https://developer.android.com/reference/kotlin/androidx/health/connect/client/testing/stubs/MutableStub<https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-any/index.html?, R>` | `<R : https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-any/index.html> https://developer.android.com/reference/kotlin/androidx/health/connect/client/testing/stubs/package-summary#MutableStub(kotlin.Throwable)(defaultError: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-throwable/index.html)` Creates a new `https://developer.android.com/reference/kotlin/androidx/health/connect/client/testing/stubs/package-summary#MutableStub(kotlin.Throwable)` that throws an exception if the queue is empty. |
| `https://developer.android.com/reference/kotlin/androidx/health/connect/client/testing/stubs/MutableStub<https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-any/index.html?, R>` | `<R : https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-any/index.html> https://developer.android.com/reference/kotlin/androidx/health/connect/client/testing/stubs/package-summary#MutableStub(kotlin.Any)(defaultResponse: R?)` Creates a new `https://developer.android.com/reference/kotlin/androidx/health/connect/client/testing/stubs/package-summary#MutableStub(kotlin.Any)` with a default return value used when the queue is empty. |
| `inline https://developer.android.com/reference/kotlin/androidx/health/connect/client/testing/stubs/Stub<T, R>` | `<T : https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-any/index.html?, R : https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-any/index.html> https://developer.android.com/reference/kotlin/androidx/health/connect/client/testing/stubs/package-summary#buildStub(kotlin.Function1)(builder: https://developer.android.com/reference/kotlin/androidx/health/connect/client/testing/stubs/MutableStub<T, R>.() -> https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html)` Builder to create `https://developer.android.com/reference/kotlin/androidx/health/connect/client/testing/stubs/MutableStub`s. |
| `https://developer.android.com/reference/kotlin/androidx/health/connect/client/testing/stubs/Stub<https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-any/index.html?, R>` | `<R : https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-any/index.html> https://developer.android.com/reference/kotlin/androidx/health/connect/client/testing/stubs/package-summary#stub(kotlin.Any)(default: R?)` Creates a stub that always returns a default value. |
| `inline https://developer.android.com/reference/kotlin/androidx/health/connect/client/testing/stubs/Stub<https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-any/index.html?, R>` | `<R : https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-any/index.html> https://developer.android.com/reference/kotlin/androidx/health/connect/client/testing/stubs/package-summary#stub(kotlin.collections.Iterable,kotlin.Function0)(queue: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.collections/-iterable/index.html<R>, crossinline defaultHandler: () -> R?)` Creates a stub that returns elements from a queue or produces it using the default handler. |

## Extension functions summary

|---|---|
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | `<R : https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-any/index.html> https://developer.android.com/reference/kotlin/androidx/health/connect/client/testing/stubs/MutableStub<*, R>.https://developer.android.com/reference/kotlin/androidx/health/connect/client/testing/stubs/package-summary#(androidx.health.connect.client.testing.stubs.MutableStub).enqueue(kotlin.collections.Iterable)(values: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.collections/-iterable/index.html<R>)` Adds a new item to the `https://developer.android.com/reference/kotlin/androidx/health/connect/client/testing/stubs/MutableStub`'s queue. |
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | `<R : https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-any/index.html> https://developer.android.com/reference/kotlin/androidx/health/connect/client/testing/stubs/MutableStub<*, R>.https://developer.android.com/reference/kotlin/androidx/health/connect/client/testing/stubs/package-summary#(androidx.health.connect.client.testing.stubs.MutableStub).enqueue(kotlin.Array)(vararg values: R)` Adds a new item to the `https://developer.android.com/reference/kotlin/androidx/health/connect/client/testing/stubs/MutableStub`'s queue. |
| `operator https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | `<R : https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-any/index.html> https://developer.android.com/reference/kotlin/androidx/health/connect/client/testing/stubs/MutableStub<*, R>.https://developer.android.com/reference/kotlin/androidx/health/connect/client/testing/stubs/package-summary#(androidx.health.connect.client.testing.stubs.MutableStub).plusAssign(kotlin.Any)(value: R)` Adds a new item to the `https://developer.android.com/reference/kotlin/androidx/health/connect/client/testing/stubs/MutableStub`'s queue. |

## Top-level functions

### MutableStub

Artifact: [androidx.health.connect:connect-testing](https://developer.android.com/jetpack/androidx/releases/health-connect) [View Source](https://cs.android.com/search?q=file:androidx/health/connect/client/testing/stubs/MutableStub.kt+function:MutableStub) Added in [1.0.0-alpha04](https://developer.android.com/jetpack/androidx/releases/health-connect#1.0.0-alpha04)

```
fun <R : Any> MutableStub(defaultError: Throwable): MutableStub<Any?, R>
```

Creates a new `https://developer.android.com/reference/kotlin/androidx/health/connect/client/testing/stubs/package-summary#MutableStub(kotlin.Throwable)` that throws an exception if the queue is empty.

Consider passing a default handler instead, so the exception is not created until necessary:

```kotlin
MutableStub { throw Exception() }
```

| Parameters |
|---|---|
| `defaultError: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-throwable/index.html` | The exception to throw if the queue is empty. |

### MutableStub

Artifact: [androidx.health.connect:connect-testing](https://developer.android.com/jetpack/androidx/releases/health-connect) [View Source](https://cs.android.com/search?q=file:androidx/health/connect/client/testing/stubs/MutableStub.kt+function:MutableStub) Added in [1.0.0-alpha04](https://developer.android.com/jetpack/androidx/releases/health-connect#1.0.0-alpha04)

```
fun <R : Any> MutableStub(defaultResponse: R?): MutableStub<Any?, R>
```

Creates a new `https://developer.android.com/reference/kotlin/androidx/health/connect/client/testing/stubs/package-summary#MutableStub(kotlin.Any)` with a default return value used when the queue is empty.

| Parameters |
|---|---|
| `defaultResponse: R?` | The default return value |

### buildStub

Artifact: [androidx.health.connect:connect-testing](https://developer.android.com/jetpack/androidx/releases/health-connect) [View Source](https://cs.android.com/search?q=file:androidx/health/connect/client/testing/stubs/MutableStub.kt+function:buildStub) Added in [1.0.0-alpha04](https://developer.android.com/jetpack/androidx/releases/health-connect#1.0.0-alpha04)

```
inline fun <T : Any?, R : Any> buildStub(builder: MutableStub<T, R>.() -> Unit): Stub<T, R>
```

Builder to create `https://developer.android.com/reference/kotlin/androidx/health/connect/client/testing/stubs/MutableStub`s.

```kotlin
import androidx.health.connect.client.testing.stubs.MutableStub
import androidx.health.connect.client.testing.stubs.buildStub
import androidx.health.connect.client.testing.stubs.enqueue

val builderMutableStub =
    buildStub<Int, String> {
        enqueue("first")
        defaultHandler = { "Default" }
    }
builderMutableStub.next(request = 0) // Returns "first"
builderMutableStub.next(request = 0) // Returns "Default"
```

### stub

Artifact: [androidx.health.connect:connect-testing](https://developer.android.com/jetpack/androidx/releases/health-connect) [View Source](https://cs.android.com/search?q=file:androidx/health/connect/client/testing/stubs/Stub.kt+function:stub) Added in [1.0.0-alpha04](https://developer.android.com/jetpack/androidx/releases/health-connect#1.0.0-alpha04)

```
fun <R : Any> stub(default: R?): Stub<Any?, R>
```

Creates a stub that always returns a default value.

Example Stub where the output is fixed:

```kotlin
import androidx.health.connect.client.testing.stubs.Stub
import androidx.health.connect.client.testing.stubs.stub

val stub: Stub<Int, String> = stub("Fixed response")
stub.next(request = 0) // Returns "Fixed response"
stub.next(request = 1) // Returns "Fixed response"
```

| Parameters |
|---|---|
| `default: R?` | A response `https://developer.android.com/reference/kotlin/androidx/health/connect/client/testing/stubs/package-summary#stub(kotlin.Any)` that will be used if a response is requested |

| See also |
|---|---|
| `https://developer.android.com/reference/kotlin/androidx/health/connect/client/testing/stubs/Stub` |   |

### stub

Artifact: [androidx.health.connect:connect-testing](https://developer.android.com/jetpack/androidx/releases/health-connect) [View Source](https://cs.android.com/search?q=file:androidx/health/connect/client/testing/stubs/Stub.kt+function:stub)

```
inline fun <R : Any> stub(
    queue: Iterable<R> = emptyList(),
    crossinline defaultHandler: () -> R? = { null }
): Stub<Any?, R>
```

Creates a stub that returns elements from a queue or produces it using the default handler.

Example Stub where the output is fixed:

```kotlin
import androidx.health.connect.client.testing.stubs.Stub
import androidx.health.connect.client.testing.stubs.stub

val stub: Stub<Int, String> = stub("Fixed response")
stub.next(request = 0) // Returns "Fixed response"
stub.next(request = 1) // Returns "Fixed response"
```

Example Stub with 2 elements in a queue that once consumed returns a fixed response :

```kotlin
import androidx.health.connect.client.testing.stubs.Stub
import androidx.health.connect.client.testing.stubs.stub

val stub: Stub<Int, String> = stub(listOf("zero", "one")) { "Default" }
stub.next(request = 0) // Returns "zero"
stub.next(request = 0) // Returns "one"
stub.next(request = 0) // Returns "Default"
```

Example Stub that throws an exception when used:

```kotlin
import androidx.health.connect.client.testing.stubs.Stub
import androidx.health.connect.client.testing.stubs.stub

val stubWithException: Stub<Int, String> = stub { throw Exception() }
stubWithException.next(request = 0) // Throws exception
stubWithException.next(request = 0) // Throws exception
```

Example Stub with 1 element in a queue that once consumed throws an exception when used:

```kotlin
import androidx.health.connect.client.testing.stubs.Stub
import androidx.health.connect.client.testing.stubs.stub

val stubWithQueueAndException: Stub<Int, String> =
    stub(queue = listOf("first")) { throw Exception() }
stubWithQueueAndException.next(request = 0) // Returns "first"
stubWithQueueAndException.next(request = 0) // Throws exception
```

| Parameters |
|---|---|
| `queue: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.collections/-iterable/index.html<R> = emptyList()` | a queue of items that has precedence over the default handler \* |
| `crossinline defaultHandler: () -> R? = { null }` | Function that produces a response `https://developer.android.com/reference/kotlin/androidx/health/connect/client/testing/stubs/package-summary#stub(kotlin.collections.Iterable,kotlin.Function0)`. Used when a response is requested and the queue is empty. |

## Extension functions

### MutableStub.enqueue

Artifact: [androidx.health.connect:connect-testing](https://developer.android.com/jetpack/androidx/releases/health-connect) [View Source](https://cs.android.com/search?q=file:androidx/health/connect/client/testing/stubs/MutableStub.kt+function:enqueue)

```
fun <R : Any> MutableStub<*, R>.enqueue(values: Iterable<R>): Unit
```

Adds a new item to the `https://developer.android.com/reference/kotlin/androidx/health/connect/client/testing/stubs/MutableStub`'s queue.

```kotlin
import androidx.health.connect.client.testing.stubs.MutableStub
import androidx.health.connect.client.testing.stubs.buildStub
import androidx.health.connect.client.testing.stubs.enqueue

val builderMutableStub =
    buildStub<Int, String> {
        enqueue("first")
        defaultHandler = { "Default" }
    }
builderMutableStub.next(request = 0) // Returns "first"
builderMutableStub.next(request = 0) // Returns "Default"
```

```kotlin
import androidx.health.connect.client.testing.stubs.MutableStub
import androidx.health.connect.client.testing.stubs.enqueue

val mutableStub = MutableStub<Int, String>(defaultHandler = { it.toString() })
mutableStub.enqueue("zero")
mutableStub += "one" // += is equivalent to enqueue()
mutableStub.next(request = 42) // Returns "zero"
mutableStub.next(request = 42) // Returns "one"
mutableStub.next(request = 42) // Returns "42"

// A new response can be enqueued at any time
mutableStub.enqueue("new")
// The default handler can also change at any time
mutableStub.defaultHandler = { (it + 1).toString() }
mutableStub.next(request = 42) // Returns "new"
mutableStub.next(request = 42) // Returns "43"
```

### MutableStub.enqueue

Artifact: [androidx.health.connect:connect-testing](https://developer.android.com/jetpack/androidx/releases/health-connect) [View Source](https://cs.android.com/search?q=file:androidx/health/connect/client/testing/stubs/MutableStub.kt+function:enqueue) Added in [1.0.0-alpha04](https://developer.android.com/jetpack/androidx/releases/health-connect#1.0.0-alpha04)

```
fun <R : Any> MutableStub<*, R>.enqueue(vararg values: R): Unit
```

Adds a new item to the `https://developer.android.com/reference/kotlin/androidx/health/connect/client/testing/stubs/MutableStub`'s queue.

```kotlin
import androidx.health.connect.client.testing.stubs.MutableStub
import androidx.health.connect.client.testing.stubs.buildStub
import androidx.health.connect.client.testing.stubs.enqueue

val builderMutableStub =
    buildStub<Int, String> {
        enqueue("first")
        defaultHandler = { "Default" }
    }
builderMutableStub.next(request = 0) // Returns "first"
builderMutableStub.next(request = 0) // Returns "Default"
```

```kotlin
import androidx.health.connect.client.testing.stubs.MutableStub
import androidx.health.connect.client.testing.stubs.enqueue

val mutableStub = MutableStub<Int, String>(defaultHandler = { it.toString() })
mutableStub.enqueue("zero")
mutableStub += "one" // += is equivalent to enqueue()
mutableStub.next(request = 42) // Returns "zero"
mutableStub.next(request = 42) // Returns "one"
mutableStub.next(request = 42) // Returns "42"

// A new response can be enqueued at any time
mutableStub.enqueue("new")
// The default handler can also change at any time
mutableStub.defaultHandler = { (it + 1).toString() }
mutableStub.next(request = 42) // Returns "new"
mutableStub.next(request = 42) // Returns "43"
```

### MutableStub.plusAssign

Artifact: [androidx.health.connect:connect-testing](https://developer.android.com/jetpack/androidx/releases/health-connect) [View Source](https://cs.android.com/search?q=file:androidx/health/connect/client/testing/stubs/MutableStub.kt+function:plusAssign) Added in [1.0.0-alpha04](https://developer.android.com/jetpack/androidx/releases/health-connect#1.0.0-alpha04)

```
operator fun <R : Any> MutableStub<*, R>.plusAssign(value: R): Unit
```

Adds a new item to the `https://developer.android.com/reference/kotlin/androidx/health/connect/client/testing/stubs/MutableStub`'s queue.

```kotlin
import androidx.health.connect.client.testing.stubs.MutableStub
import androidx.health.connect.client.testing.stubs.enqueue

val mutableStub = MutableStub<Int, String>(defaultHandler = { it.toString() })
mutableStub.enqueue("zero")
mutableStub += "one" // += is equivalent to enqueue()
mutableStub.next(request = 42) // Returns "zero"
mutableStub.next(request = 42) // Returns "one"
mutableStub.next(request = 42) // Returns "42"

// A new response can be enqueued at any time
mutableStub.enqueue("new")
// The default handler can also change at any time
mutableStub.defaultHandler = { (it + 1).toString() }
mutableStub.next(request = 42) // Returns "new"
mutableStub.next(request = 42) // Returns "43"
```