StubKt

Added in 1.0.0-alpha01

public final class StubKt


Summary

Public methods

static final @NonNull Stub<Object, @NonNull R>
<R extends Object> stub(R default)

Creates a stub that always returns a default value.

static final @NonNull Stub<Object, @NonNull R>
<R extends Object> stub(
    @NonNull Iterable<@NonNull R> queue,
    @NonNull Function0<R> defaultHandler
)

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

Public methods

public static final @NonNull Stub<Object, @NonNull R> <R extends Object> stub(R default)

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
R default

A response R that will be used if a response is requested

See also
Stub
public static final @NonNull Stub<Object, @NonNull R> <R extends Object> stub(
    @NonNull Iterable<@NonNull R> queue,
    @NonNull Function0<R> defaultHandler
)

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
@NonNull Iterable<@NonNull R> queue

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

@NonNull Function0<R> defaultHandler

Function that produces a response R. Used when a response is requested and the queue is empty.