public final class MutableStub<T extends Object, R extends Object> implements 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 extends Object, R extends Object> MutableStub(
    @NonNull Function1<@NonNull T, R> defaultHandler
)

Public methods

final @NonNull Function1<@NonNull T, R>

Function that maps a request T to a response R.

R
next(@NonNull T request)

Returns the next item.

final void
setDefaultHandler(@NonNull Function1<@NonNull T, R> defaultHandler)

Function that maps a request T to a response R.

Extension functions

final void
<R extends Object> MutableStubKt.enqueue(
    @NonNull MutableStub<@NonNull ?, @NonNull R> receiver,
    @NonNull Iterable<@NonNull R> values
)

Adds a new item to the MutableStub's queue.

final void
<R extends Object> MutableStubKt.enqueue(
    @NonNull MutableStub<@NonNull ?, @NonNull R> receiver,
    @NonNull R values
)

Adds a new item to the MutableStub's queue.

final void
<R extends Object> MutableStubKt.plusAssign(
    @NonNull MutableStub<@NonNull ?, @NonNull R> receiver,
    @NonNull R value
)

Adds a new item to the MutableStub's queue.

Public constructors

MutableStub

public <T extends Object, R extends Object> MutableStub(
    @NonNull Function1<@NonNull T, R> defaultHandler
)

Public methods

getDefaultHandler

Added in 1.0.0-alpha01
public final @NonNull Function1<@NonNull T, R> getDefaultHandler()

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

next

Added in 1.0.0-alpha01
public R next(@NonNull T request)

Returns the next item.

setDefaultHandler

Added in 1.0.0-alpha01
public final void setDefaultHandler(@NonNull Function1<@NonNull T, R> defaultHandler)

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

Extension functions

MutableStubKt.enqueue

public final void <R extends Object> MutableStubKt.enqueue(
    @NonNull MutableStub<@NonNull ?, @NonNull R> receiver,
    @NonNull Iterable<@NonNull R> values
)

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"

MutableStubKt.enqueue

public final void <R extends Object> MutableStubKt.enqueue(
    @NonNull MutableStub<@NonNull ?, @NonNull R> receiver,
    @NonNull R values
)

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"

MutableStubKt.plusAssign

public final void <R extends Object> MutableStubKt.plusAssign(
    @NonNull MutableStub<@NonNull ?, @NonNull R> receiver,
    @NonNull R value
)

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"