class MutableStub<T : Any?, R : Any> : Stub


A Stub whose defaultHandler and queue can be mutated at any point.

Example MutableStub with a default value and a new item enqueued after consumption:

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

val simpleMutableStub = MutableStub("default")
simpleMutableStub.next(request = 0) // Returns "default"

simpleMutableStub.enqueue("new")

simpleMutableStub.next(request = 0) // Returns "new"
simpleMutableStub.next(request = 0) // Returns "default"

Example MutableStub with an item in the queue and a default value, alternative construction:

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

val mutableStub =
    MutableStub<Any, String>().apply {
        enqueue("first")
        defaultHandler = { "Default" }
    }
mutableStub.next(request = 0) // Returns "first"
mutableStub.next(request = 0) // Returns "Default"

Example Mutable stub with 1 element in a queue that once consumed returns a fixed response which depends on the input:

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"

Example Mutable stub created with the buildStub builder:

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"
See also
Stub

Summary

Public constructors

<T : Any?, R : Any> MutableStub(defaultHandler: (T) -> R?)

Public functions

open R?
next(request: T)

Returns the next item.

Public properties

(T) -> R?

Function that maps a request T to a response R.

Extension functions

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

Adds a new item to the MutableStub's queue.

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

Adds a new item to the MutableStub's queue.

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

Adds a new item to the MutableStub's queue.

Public constructors

MutableStub

<T : Any?, R : Any> MutableStub(defaultHandler: (T) -> R? = { null })

Public functions

next

Added in 1.0.0-alpha01
open fun next(request: T): R?

Returns the next item.

Public properties

defaultHandler

Added in 1.0.0-alpha01
var defaultHandler: (T) -> R?

Function that maps a request T to a response R. Used when the queue is empty.

Extension functions

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

Adds a new item to the MutableStub's queue.

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"
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"
fun <R : Any> MutableStub<*, R>.enqueue(vararg values: R): Unit

Adds a new item to the MutableStub's queue.

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"
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"
operator fun <R : Any> MutableStub<*, R>.plusAssign(value: R): Unit

Adds a new item to the MutableStub's queue.

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"