FakeHealthConnectClientOverrides


public final 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(
    Stub<@NonNull String, @NonNull ChangesResponse> getChanges,
    Stub<@NonNull ChangesTokenRequest, @NonNull String> getChangesToken,
    Stub<@NonNull ReadRecordsRequest<@NonNull ?>, @NonNull ReadRecordsResponse<@NonNull ?>> readRecords,
    Stub<@NonNull String, @NonNull ReadRecordResponse<@NonNull ?>> readRecord,
    Stub<@NonNull List<@NonNull ?>, @NonNull InsertRecordsResponse> insertRecords,
    Stub<@NonNull Object, @NonNull Void> updateRecords,
    Stub<@NonNull Object, @NonNull Void> deleteRecords,
    Stub<@NonNull AggregateRequest, @NonNull AggregationResult> aggregate,
    Stub<@NonNull AggregateGroupByDurationRequest, @NonNull List<@NonNull AggregationResultGroupedByDuration>> aggregateGroupByDuration,
    Stub<@NonNull AggregateGroupByPeriodRequest, @NonNull List<@NonNull AggregationResultGroupedByPeriod>> aggregateGroupByPeriod
)

Public methods

final Stub<@NonNull AggregateRequest, @NonNull AggregationResult>

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

final Stub<@NonNull AggregateGroupByDurationRequest, @NonNull List<@NonNull AggregationResultGroupedByDuration>>

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

final Stub<@NonNull AggregateGroupByPeriodRequest, @NonNull List<@NonNull AggregationResultGroupedByPeriod>>

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

final Stub<@NonNull Object, @NonNull Void>

A Stub used only to throw exceptions in deleteRecords.

final Stub<@NonNull String, @NonNull ChangesResponse>

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

final Stub<@NonNull ChangesTokenRequest, @NonNull String>

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

final Stub<@NonNull List<@NonNull ?>, @NonNull InsertRecordsResponse>

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

final Stub<@NonNull String, @NonNull ReadRecordResponse<@NonNull ?>>

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

final Stub<@NonNull ReadRecordsRequest<@NonNull ?>, @NonNull ReadRecordsResponse<@NonNull ?>>

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

final Stub<@NonNull Object, @NonNull Void>

A Stub used only to throw exceptions in updateRecords.

final void

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

final void

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

final void

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

final void

A Stub used only to throw exceptions in deleteRecords.

final void

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

final void

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

final void

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

final void

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

final void

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

final void

A Stub used only to throw exceptions in updateRecords.

Public constructors

FakeHealthConnectClientOverrides

public FakeHealthConnectClientOverrides(
    Stub<@NonNull String, @NonNull ChangesResponse> getChanges,
    Stub<@NonNull ChangesTokenRequest, @NonNull String> getChangesToken,
    Stub<@NonNull ReadRecordsRequest<@NonNull ?>, @NonNull ReadRecordsResponse<@NonNull ?>> readRecords,
    Stub<@NonNull String, @NonNull ReadRecordResponse<@NonNull ?>> readRecord,
    Stub<@NonNull List<@NonNull ?>, @NonNull InsertRecordsResponse> insertRecords,
    Stub<@NonNull Object, @NonNull Void> updateRecords,
    Stub<@NonNull Object, @NonNull Void> deleteRecords,
    Stub<@NonNull AggregateRequest, @NonNull AggregationResult> aggregate,
    Stub<@NonNull AggregateGroupByDurationRequest, @NonNull List<@NonNull AggregationResultGroupedByDuration>> aggregateGroupByDuration,
    Stub<@NonNull AggregateGroupByPeriodRequest, @NonNull List<@NonNull AggregationResultGroupedByPeriod>> aggregateGroupByPeriod
)
Parameters
Stub<@NonNull String, @NonNull ChangesResponse> getChanges

A Stub used only to throw exceptions in getChanges.

Stub<@NonNull ChangesTokenRequest, @NonNull String> getChangesToken

A Stub used only to throw exceptions in getChangesToken.

Stub<@NonNull ReadRecordsRequest<@NonNull ?>, @NonNull ReadRecordsResponse<@NonNull ?>> readRecords

A Stub used only to throw exceptions in readRecords.

Stub<@NonNull String, @NonNull ReadRecordResponse<@NonNull ?>> readRecord

A Stub used only to throw exceptions in readRecord.

Stub<@NonNull List<@NonNull ?>, @NonNull InsertRecordsResponse> insertRecords

A Stub used only to throw exceptions in insertRecords.

Stub<@NonNull Object, @NonNull Void> updateRecords

A Stub used only to throw exceptions in updateRecords.

Stub<@NonNull Object, @NonNull Void> deleteRecords

A Stub used only to throw exceptions in deleteRecords.

Stub<@NonNull AggregateRequest, @NonNull AggregationResult> aggregate

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

Stub<@NonNull AggregateGroupByDurationRequest, @NonNull List<@NonNull AggregationResultGroupedByDuration>> aggregateGroupByDuration

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

Stub<@NonNull AggregateGroupByPeriodRequest, @NonNull List<@NonNull AggregationResultGroupedByPeriod>> aggregateGroupByPeriod

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

Public methods

getAggregate

Added in 1.0.0-alpha01
public final Stub<@NonNull AggregateRequest, @NonNull AggregationResultgetAggregate()

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)

getAggregateGroupByDuration

Added in 1.0.0-alpha01
public final Stub<@NonNull AggregateGroupByDurationRequest, @NonNull List<@NonNull AggregationResultGroupedByDuration>> getAggregateGroupByDuration()

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)

getAggregateGroupByPeriod

Added in 1.0.0-alpha01
public final Stub<@NonNull AggregateGroupByPeriodRequest, @NonNull List<@NonNull AggregationResultGroupedByPeriod>> getAggregateGroupByPeriod()

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)

getDeleteRecords

Added in 1.0.0-alpha01
public final Stub<@NonNull Object, @NonNull VoidgetDeleteRecords()

A Stub used only to throw exceptions in deleteRecords.

getGetChanges

Added in 1.0.0-alpha01
public final Stub<@NonNull String, @NonNull ChangesResponsegetGetChanges()

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

getGetChangesToken

Added in 1.0.0-alpha01
public final Stub<@NonNull ChangesTokenRequest, @NonNull StringgetGetChangesToken()

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

getInsertRecords

Added in 1.0.0-alpha01
public final Stub<@NonNull List<@NonNull ?>, @NonNull InsertRecordsResponsegetInsertRecords()

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

getReadRecord

Added in 1.0.0-alpha01
public final Stub<@NonNull String, @NonNull ReadRecordResponse<@NonNull ?>> getReadRecord()

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

getReadRecords

Added in 1.0.0-alpha01
public final Stub<@NonNull ReadRecordsRequest<@NonNull ?>, @NonNull ReadRecordsResponse<@NonNull ?>> getReadRecords()

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

getUpdateRecords

Added in 1.0.0-alpha01
public final Stub<@NonNull Object, @NonNull VoidgetUpdateRecords()

A Stub used only to throw exceptions in updateRecords.

setAggregate

public final void setAggregate(
    Stub<@NonNull AggregateRequest, @NonNull AggregationResult> aggregate
)

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)

setAggregateGroupByDuration

public final void setAggregateGroupByDuration(
    Stub<@NonNull AggregateGroupByDurationRequest, @NonNull List<@NonNull AggregationResultGroupedByDuration>> aggregateGroupByDuration
)

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)

setAggregateGroupByPeriod

public final void setAggregateGroupByPeriod(
    Stub<@NonNull AggregateGroupByPeriodRequest, @NonNull List<@NonNull AggregationResultGroupedByPeriod>> aggregateGroupByPeriod
)

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)

setDeleteRecords

public final void setDeleteRecords(Stub<@NonNull Object, @NonNull Void> deleteRecords)

A Stub used only to throw exceptions in deleteRecords.

setGetChanges

public final void setGetChanges(Stub<@NonNull String, @NonNull ChangesResponse> getChanges)

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

setGetChangesToken

public final void setGetChangesToken(
    Stub<@NonNull ChangesTokenRequest, @NonNull String> getChangesToken
)

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

setInsertRecords

public final void setInsertRecords(
    Stub<@NonNull List<@NonNull ?>, @NonNull InsertRecordsResponse> insertRecords
)

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

setReadRecord

public final void setReadRecord(
    Stub<@NonNull String, @NonNull ReadRecordResponse<@NonNull ?>> readRecord
)

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

setReadRecords

public final void setReadRecords(
    Stub<@NonNull ReadRecordsRequest<@NonNull ?>, @NonNull ReadRecordsResponse<@NonNull ?>> readRecords
)

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

setUpdateRecords

public final void setUpdateRecords(Stub<@NonNull Object, @NonNull Void> updateRecords)

A Stub used only to throw exceptions in updateRecords.