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<Nothing?, Nothing>?,
    getChangesToken: Stub<Nothing?, Nothing>?,
    readRecords: Stub<Nothing?, Nothing>?,
    readRecord: Stub<Nothing?, Nothing>?,
    insertRecords: Stub<Nothing?, Nothing>?,
    updateRecords: Stub<Nothing?, Nothing>?,
    deleteRecords: Stub<Nothing?, Nothing>?,
    aggregate: Stub<AggregateRequestAggregationResult>?,
    aggregateGroupByDuration: Stub<AggregateGroupByDurationRequestList<AggregationResultGroupedByDuration>>?,
    aggregateGroupByPeriod: Stub<AggregateGroupByPeriodRequestList<AggregationResultGroupedByPeriod>>?
)

Public properties

Stub<AggregateRequestAggregationResult>?

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

Stub<AggregateGroupByDurationRequestList<AggregationResultGroupedByDuration>>?

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

Stub<AggregateGroupByPeriodRequestList<AggregationResultGroupedByPeriod>>?

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

Stub<Nothing?, Nothing>?

A Stub used only to throw exceptions in deleteRecords.

Stub<Nothing?, Nothing>?

A Stub used only to throw exceptions in getChanges.

Stub<Nothing?, Nothing>?

A Stub used only to throw exceptions in getChangesToken.

Stub<Nothing?, Nothing>?

A Stub used only to throw exceptions in insertRecords.

Stub<Nothing?, Nothing>?

A Stub used only to throw exceptions in readRecord.

Stub<Nothing?, Nothing>?

A Stub used only to throw exceptions in readRecords.

Stub<Nothing?, Nothing>?

A Stub used only to throw exceptions in updateRecords.

Public constructors

FakeHealthConnectClientOverrides

FakeHealthConnectClientOverrides(
    getChanges: Stub<Nothing?, Nothing>? = null,
    getChangesToken: Stub<Nothing?, Nothing>? = null,
    readRecords: Stub<Nothing?, Nothing>? = null,
    readRecord: Stub<Nothing?, Nothing>? = null,
    insertRecords: Stub<Nothing?, Nothing>? = null,
    updateRecords: Stub<Nothing?, Nothing>? = null,
    deleteRecords: Stub<Nothing?, Nothing>? = null,
    aggregate: Stub<AggregateRequestAggregationResult>? = null,
    aggregateGroupByDuration: Stub<AggregateGroupByDurationRequestList<AggregationResultGroupedByDuration>>? = null,
    aggregateGroupByPeriod: Stub<AggregateGroupByPeriodRequestList<AggregationResultGroupedByPeriod>>? = null
)
Parameters
getChanges: Stub<Nothing?, Nothing>? = null

A Stub used only to throw exceptions in getChanges.

getChangesToken: Stub<Nothing?, Nothing>? = null

A Stub used only to throw exceptions in getChangesToken.

readRecords: Stub<Nothing?, Nothing>? = null

A Stub used only to throw exceptions in readRecords.

readRecord: Stub<Nothing?, Nothing>? = null

A Stub used only to throw exceptions in readRecord.

insertRecords: Stub<Nothing?, Nothing>? = null

A Stub used only to throw exceptions in insertRecords.

updateRecords: Stub<Nothing?, Nothing>? = null

A Stub used only to throw exceptions in updateRecords.

deleteRecords: Stub<Nothing?, Nothing>? = null

A Stub used only to throw exceptions in deleteRecords.

aggregate: Stub<AggregateRequestAggregationResult>? = null

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

aggregateGroupByDuration: Stub<AggregateGroupByDurationRequestList<AggregationResultGroupedByDuration>>? = null

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

aggregateGroupByPeriod: Stub<AggregateGroupByPeriodRequestList<AggregationResultGroupedByPeriod>>? = null

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

Public properties

aggregate

Added in 1.0.0-alpha01
var aggregateStub<AggregateRequestAggregationResult>?

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)

aggregateGroupByDuration

Added in 1.0.0-alpha01
var aggregateGroupByDurationStub<AggregateGroupByDurationRequestList<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)

aggregateGroupByPeriod

Added in 1.0.0-alpha01
var aggregateGroupByPeriodStub<AggregateGroupByPeriodRequestList<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)

deleteRecords

Added in 1.0.0-alpha01
var deleteRecordsStub<Nothing?, Nothing>?

A Stub used only to throw exceptions in deleteRecords.

getChanges

Added in 1.0.0-alpha01
var getChangesStub<Nothing?, Nothing>?

A Stub used only to throw exceptions in getChanges.

getChangesToken

Added in 1.0.0-alpha01
var getChangesTokenStub<Nothing?, Nothing>?

A Stub used only to throw exceptions in getChangesToken.

insertRecords

Added in 1.0.0-alpha01
var insertRecordsStub<Nothing?, Nothing>?

A Stub used only to throw exceptions in insertRecords.

readRecord

Added in 1.0.0-alpha01
var readRecordStub<Nothing?, Nothing>?

A Stub used only to throw exceptions in readRecord.

readRecords

Added in 1.0.0-alpha01
var readRecordsStub<Nothing?, Nothing>?

A Stub used only to throw exceptions in readRecords.

updateRecords

Added in 1.0.0-alpha01
var updateRecordsStub<Nothing?, Nothing>?

A Stub used only to throw exceptions in updateRecords.