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<Void, @NonNull Void> getChanges,
    Stub<Void, @NonNull Void> getChangesToken,
    Stub<Void, @NonNull Void> readRecords,
    Stub<Void, @NonNull Void> readRecord,
    Stub<Void, @NonNull Void> insertRecords,
    Stub<Void, @NonNull Void> updateRecords,
    Stub<Void, @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<Void, @NonNull Void>

A Stub used only to throw exceptions in deleteRecords.

final Stub<Void, @NonNull Void>

A Stub used only to throw exceptions in getChanges.

final Stub<Void, @NonNull Void>

A Stub used only to throw exceptions in getChangesToken.

final Stub<Void, @NonNull Void>

A Stub used only to throw exceptions in insertRecords.

final Stub<Void, @NonNull Void>

A Stub used only to throw exceptions in readRecord.

final Stub<Void, @NonNull Void>

A Stub used only to throw exceptions in readRecords.

final Stub<Void, @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
setDeleteRecords(Stub<Void, @NonNull Void> deleteRecords)
final void
final void
setGetChangesToken(Stub<Void, @NonNull Void> getChangesToken)
final void
setInsertRecords(Stub<Void, @NonNull Void> insertRecords)
final void
final void
setReadRecords(Stub<Void, @NonNull Void> readRecords)
final void
setUpdateRecords(Stub<Void, @NonNull Void> updateRecords)

Public constructors

FakeHealthConnectClientOverrides

public FakeHealthConnectClientOverrides(
    Stub<Void, @NonNull Void> getChanges,
    Stub<Void, @NonNull Void> getChangesToken,
    Stub<Void, @NonNull Void> readRecords,
    Stub<Void, @NonNull Void> readRecord,
    Stub<Void, @NonNull Void> insertRecords,
    Stub<Void, @NonNull Void> updateRecords,
    Stub<Void, @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<Void, @NonNull Void> getChanges

A Stub used only to throw exceptions in getChanges.

Stub<Void, @NonNull Void> getChangesToken

A Stub used only to throw exceptions in getChangesToken.

Stub<Void, @NonNull Void> readRecords

A Stub used only to throw exceptions in readRecords.

Stub<Void, @NonNull Void> readRecord

A Stub used only to throw exceptions in readRecord.

Stub<Void, @NonNull Void> insertRecords

A Stub used only to throw exceptions in insertRecords.

Stub<Void, @NonNull Void> updateRecords

A Stub used only to throw exceptions in updateRecords.

Stub<Void, @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<Void, @NonNull VoidgetDeleteRecords()

A Stub used only to throw exceptions in deleteRecords.

getGetChanges

Added in 1.0.0-alpha01
public final Stub<Void, @NonNull VoidgetGetChanges()

A Stub used only to throw exceptions in getChanges.

getGetChangesToken

Added in 1.0.0-alpha01
public final Stub<Void, @NonNull VoidgetGetChangesToken()

A Stub used only to throw exceptions in getChangesToken.

getInsertRecords

Added in 1.0.0-alpha01
public final Stub<Void, @NonNull VoidgetInsertRecords()

A Stub used only to throw exceptions in insertRecords.

getReadRecord

Added in 1.0.0-alpha01
public final Stub<Void, @NonNull VoidgetReadRecord()

A Stub used only to throw exceptions in readRecord.

getReadRecords

Added in 1.0.0-alpha01
public final Stub<Void, @NonNull VoidgetReadRecords()

A Stub used only to throw exceptions in readRecords.

getUpdateRecords

Added in 1.0.0-alpha01
public final Stub<Void, @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<Void, @NonNull Void> deleteRecords)
Parameters
Stub<Void, @NonNull Void> deleteRecords

A Stub used only to throw exceptions in deleteRecords.

setGetChanges

public final void setGetChanges(Stub<Void, @NonNull Void> getChanges)
Parameters
Stub<Void, @NonNull Void> getChanges

A Stub used only to throw exceptions in getChanges.

setGetChangesToken

public final void setGetChangesToken(Stub<Void, @NonNull Void> getChangesToken)
Parameters
Stub<Void, @NonNull Void> getChangesToken

A Stub used only to throw exceptions in getChangesToken.

setInsertRecords

public final void setInsertRecords(Stub<Void, @NonNull Void> insertRecords)
Parameters
Stub<Void, @NonNull Void> insertRecords

A Stub used only to throw exceptions in insertRecords.

setReadRecord

public final void setReadRecord(Stub<Void, @NonNull Void> readRecord)
Parameters
Stub<Void, @NonNull Void> readRecord

A Stub used only to throw exceptions in readRecord.

setReadRecords

public final void setReadRecords(Stub<Void, @NonNull Void> readRecords)
Parameters
Stub<Void, @NonNull Void> readRecords

A Stub used only to throw exceptions in readRecords.

setUpdateRecords

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

A Stub used only to throw exceptions in updateRecords.