androidx.health.connect.client.testing.stubs


Interfaces

Stub

Represents a stub mapping from a request T to a response R.

Classes

MutableStub

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

Top-level functions summary

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

Creates a new MutableStub that throws an exception if the queue is empty.

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

Creates a new MutableStub with a default return value used when the queue is empty.

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

Builder to create MutableStubs.

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

Creates a stub that always returns a default value.

inline Stub<Any?, R>
<R : Any> stub(queue: Iterable<R>, crossinline defaultHandler: () -> R?)

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

Extension functions summary

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.

Top-level functions

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

Creates a new MutableStub that throws an exception if the queue is empty.

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

MutableStub { throw Exception() }
Parameters
defaultError: Throwable

The exception to throw if the queue is empty.

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

Creates a new MutableStub with a default return value used when the queue is empty.

Parameters
defaultResponse: R?

The default return value

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

Builder to create MutableStubs.

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"
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:

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 R that will be used if a response is requested

See also
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:

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 :

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:

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:

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: Iterable<R> = emptyList()

a queue of items that has precedence over the default handler *

crossinline defaultHandler: () -> R? = { null }

Function that produces a response R. Used when a response is requested and 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"