FakeHealthConnectClientOverrides


class FakeHealthConnectClientOverrides


Used to override or intercept responses to emulate scenarios that FakeHealthConnectClient doesn't support.

Every call in FakeHealthConnectClient can be overridden.

For example:

import androidx.health.connect.client.testing.stubs.stub

// Sets a default return value for client.aggregate() and a queue with one item.
client.overrides.aggregate = stub(queue = listOf(resultOnce)) { result }
import androidx.health.connect.client.aggregate.AggregationResult
import androidx.health.connect.client.testing.AggregationResult
import androidx.health.connect.client.testing.stubs.MutableStub
import androidx.health.connect.client.testing.stubs.enqueue

// Sets a default exception that will be thrown for client.aggregate().
val aggregationStub = MutableStub<AggregationResult>(exception)
client.overrides.aggregate = aggregationStub

// Only the first call to client.aggregate() will return this result. Subsequent calls will
// throw the default.
aggregationStub.enqueue(result)

// Setting a default response removes the default exception.
aggregationStub.defaultHandler = { AggregationResult() }
import androidx.health.connect.client.aggregate.AggregationResult
import androidx.health.connect.client.records.ExerciseSessionRecord
import androidx.health.connect.client.records.HeartRateRecord
import androidx.health.connect.client.testing.AggregationResult
import androidx.health.connect.client.testing.stubs.stub

val result =
    AggregationResult(
        metrics =
            buildMap {
                put(HeartRateRecord.BPM_AVG, 74.0)
                put(ExerciseSessionRecord.EXERCISE_DURATION_TOTAL, Duration.ofMinutes(30))
            }
    )
client.overrides.aggregate = stub(result)
import androidx.health.connect.client.aggregate.AggregationResult
import androidx.health.connect.client.aggregate.AggregationResultGroupedByDuration
import androidx.health.connect.client.testing.AggregationResult
import androidx.health.connect.client.testing.stubs.stub

val result = buildList {
    add(
        AggregationResultGroupedByDuration(
            aggregationResult1,
            startTime1,
            endTime1,
            ZoneOffset.UTC,
        )
    )
    add(
        AggregationResultGroupedByDuration(
            aggregationResult2,
            startTime2,
            endTime2,
            ZoneOffset.UTC,
        )
    )
}
client.overrides.aggregateGroupByDuration = stub(default = result)
import androidx.health.connect.client.aggregate.AggregationResult
import androidx.health.connect.client.aggregate.AggregationResultGroupedByPeriod
import androidx.health.connect.client.testing.AggregationResult
import androidx.health.connect.client.testing.stubs.stub

val result = buildList {
    add(AggregationResultGroupedByPeriod(aggregationResult1, startTime1, endTime1))
    add(AggregationResultGroupedByPeriod(aggregationResult2, startTime2, endTime2))
}
client.overrides.aggregateGroupByPeriod = stub(default = result)
import androidx.health.connect.client.aggregate.AggregationResult
import androidx.health.connect.client.records.ExerciseSessionRecord
import androidx.health.connect.client.records.HeartRateRecord
import androidx.health.connect.client.testing.AggregationResult
import androidx.health.connect.client.testing.stubs.stub

val result =
    AggregationResult(
        metrics =
            buildMap {
                put(HeartRateRecord.BPM_AVG, 74.0)
                put(ExerciseSessionRecord.EXERCISE_DURATION_TOTAL, Duration.ofMinutes(30))
            }
    )
client.overrides.aggregate = stub(result)

Summary

Public constructors

FakeHealthConnectClientOverrides(
    getChanges: Stub<String, ChangesResponse>?,
    getChangesToken: Stub<ChangesTokenRequest, String>?,
    readRecords: Stub<ReadRecordsRequest<*>, ReadRecordsResponse<*>>?,
    readRecord: Stub<String, ReadRecordResponse<*>>?,
    insertRecords: Stub<List<*>, InsertRecordsResponse>?,
    updateRecords: Stub<Any, Nothing>?,
    deleteRecords: Stub<Any, Nothing>?,
    aggregate: Stub<AggregateRequest, AggregationResult>?,
    aggregateGroupByDuration: Stub<AggregateGroupByDurationRequest, List<AggregationResultGroupedByDuration>>?,
    aggregateGroupByPeriod: Stub<AggregateGroupByPeriodRequest, List<AggregationResultGroupedByPeriod>>?
)

Public properties

Stub<AggregateRequest, AggregationResult>?

A Stub used to set the next responses used in FakeHealthConnectClient.aggregate.

Stub<AggregateGroupByDurationRequest, List<AggregationResultGroupedByDuration>>?

A Stub used to set the next responses used in FakeHealthConnectClient.aggregateGroupByDuration.

Stub<AggregateGroupByPeriodRequest, List<AggregationResultGroupedByPeriod>>?

A Stub used to set the next responses used in FakeHealthConnectClient.aggregateGroupByPeriod.

Stub<Any, Nothing>?

A Stub used only to throw exceptions in deleteRecords.

Stub<String, ChangesResponse>?

A Stub used to set the next responses used in getChanges.

Stub<ChangesTokenRequest, String>?

A Stub used to set the next responses used in getChangesToken.

Stub<List<*>, InsertRecordsResponse>?

A Stub used to set the next responses used in insertRecords.

Stub<String, ReadRecordResponse<*>>?

A Stub used to set the next responses used in readRecord.

Stub<ReadRecordsRequest<*>, ReadRecordsResponse<*>>?

A Stub used to set the next responses used in readRecords.

Stub<Any, Nothing>?

A Stub used only to throw exceptions in updateRecords.

Public constructors

FakeHealthConnectClientOverrides

FakeHealthConnectClientOverrides(
    getChanges: Stub<String, ChangesResponse>? = null,
    getChangesToken: Stub<ChangesTokenRequest, String>? = null,
    readRecords: Stub<ReadRecordsRequest<*>, ReadRecordsResponse<*>>? = null,
    readRecord: Stub<String, ReadRecordResponse<*>>? = null,
    insertRecords: Stub<List<*>, InsertRecordsResponse>? = null,
    updateRecords: Stub<Any, Nothing>? = null,
    deleteRecords: Stub<Any, Nothing>? = null,
    aggregate: Stub<AggregateRequest, AggregationResult>? = null,
    aggregateGroupByDuration: Stub<AggregateGroupByDurationRequest, List<AggregationResultGroupedByDuration>>? = null,
    aggregateGroupByPeriod: Stub<AggregateGroupByPeriodRequest, List<AggregationResultGroupedByPeriod>>? = null
)
Parameters
getChanges: Stub<String, ChangesResponse>? = null

A Stub used only to throw exceptions in getChanges.

getChangesToken: Stub<ChangesTokenRequest, String>? = null

A Stub used only to throw exceptions in getChangesToken.

readRecords: Stub<ReadRecordsRequest<*>, ReadRecordsResponse<*>>? = null

A Stub used only to throw exceptions in readRecords.

readRecord: Stub<String, ReadRecordResponse<*>>? = null

A Stub used only to throw exceptions in readRecord.

insertRecords: Stub<List<*>, InsertRecordsResponse>? = null

A Stub used only to throw exceptions in insertRecords.

updateRecords: Stub<Any, Nothing>? = null

A Stub used only to throw exceptions in updateRecords.

deleteRecords: Stub<Any, Nothing>? = null

A Stub used only to throw exceptions in deleteRecords.

aggregate: Stub<AggregateRequest, AggregationResult>? = null

A Stub used to set the next responses used in FakeHealthConnectClient.aggregate.

aggregateGroupByDuration: Stub<AggregateGroupByDurationRequest, List<AggregationResultGroupedByDuration>>? = null

A Stub used to set the next responses used in FakeHealthConnectClient.aggregateGroupByDuration.

aggregateGroupByPeriod: Stub<AggregateGroupByPeriodRequest, List<AggregationResultGroupedByPeriod>>? = null

A Stub used to set the next responses used in FakeHealthConnectClient.aggregateGroupByPeriod.

Public properties

aggregate

Added in 1.0.0-alpha04
var aggregate: Stub<AggregateRequest, AggregationResult>?

A Stub used to set the next responses used in FakeHealthConnectClient.aggregate.

import androidx.health.connect.client.aggregate.AggregationResult
import androidx.health.connect.client.records.ExerciseSessionRecord
import androidx.health.connect.client.records.HeartRateRecord
import androidx.health.connect.client.testing.AggregationResult
import androidx.health.connect.client.testing.stubs.stub

val result =
    AggregationResult(
        metrics =
            buildMap {
                put(HeartRateRecord.BPM_AVG, 74.0)
                put(ExerciseSessionRecord.EXERCISE_DURATION_TOTAL, Duration.ofMinutes(30))
            }
    )
client.overrides.aggregate = stub(result)
See also

aggregateGroupByDuration

Added in 1.0.0-alpha04
var aggregateGroupByDuration: Stub<AggregateGroupByDurationRequest, List<AggregationResultGroupedByDuration>>?

A Stub used to set the next responses used in FakeHealthConnectClient.aggregateGroupByDuration.

import androidx.health.connect.client.aggregate.AggregationResult
import androidx.health.connect.client.aggregate.AggregationResultGroupedByDuration
import androidx.health.connect.client.testing.AggregationResult
import androidx.health.connect.client.testing.stubs.stub

val result = buildList {
    add(
        AggregationResultGroupedByDuration(
            aggregationResult1,
            startTime1,
            endTime1,
            ZoneOffset.UTC,
        )
    )
    add(
        AggregationResultGroupedByDuration(
            aggregationResult2,
            startTime2,
            endTime2,
            ZoneOffset.UTC,
        )
    )
}
client.overrides.aggregateGroupByDuration = stub(default = result)
See also

aggregateGroupByPeriod

Added in 1.0.0-alpha04
var aggregateGroupByPeriod: Stub<AggregateGroupByPeriodRequest, List<AggregationResultGroupedByPeriod>>?

A Stub used to set the next responses used in FakeHealthConnectClient.aggregateGroupByPeriod.

import androidx.health.connect.client.aggregate.AggregationResult
import androidx.health.connect.client.aggregate.AggregationResultGroupedByPeriod
import androidx.health.connect.client.testing.AggregationResult
import androidx.health.connect.client.testing.stubs.stub

val result = buildList {
    add(AggregationResultGroupedByPeriod(aggregationResult1, startTime1, endTime1))
    add(AggregationResultGroupedByPeriod(aggregationResult2, startTime2, endTime2))
}
client.overrides.aggregateGroupByPeriod = stub(default = result)
See also

deleteRecords

Added in 1.0.0-alpha04
var deleteRecords: Stub<Any, Nothing>?

A Stub used only to throw exceptions in deleteRecords.

getChanges

Added in 1.0.0-alpha04
var getChanges: Stub<String, ChangesResponse>?

A Stub used to set the next responses used in getChanges.

getChangesToken

Added in 1.0.0-alpha04
var getChangesToken: Stub<ChangesTokenRequest, String>?

A Stub used to set the next responses used in getChangesToken.

insertRecords

Added in 1.0.0-alpha04
var insertRecords: Stub<List<*>, InsertRecordsResponse>?

A Stub used to set the next responses used in insertRecords.

readRecord

Added in 1.0.0-alpha04
var readRecord: Stub<String, ReadRecordResponse<*>>?

A Stub used to set the next responses used in readRecord.

readRecords

Added in 1.0.0-alpha04
var readRecords: Stub<ReadRecordsRequest<*>, ReadRecordsResponse<*>>?

A Stub used to set the next responses used in readRecords.

updateRecords

Added in 1.0.0-alpha04
var updateRecords: Stub<Any, Nothing>?

A Stub used only to throw exceptions in updateRecords.