Repository and data layer testing
Repository And Data Layer Testing Deep Dive¶
Overview¶
Repository tests validate orchestration across local cache, network, and sync logic.
Core Concepts¶
- Clear source-of-truth policy.
- Deterministic fallback rules.
- Explicit cache invalidation behavior.
Test Pyramid and Strategy¶
- Unit test orchestration branches first.
- Add integration tests for persistence/network boundary.
Tooling and Infrastructure¶
- Fake local store + fake remote API for unit speed.
- In-memory DB + MockWebServer for integration confidence.
Flaky Test Mitigation¶
- No shared singleton test state.
- Stable fixture data and repeatable ordering.
Code Examples¶
@Test
fun repository_usesCacheThenRefreshesFromNetwork() = runTest {
// Assert stale-while-revalidate behavior
}
Common Interview Questions¶
- Q: How do you test offline-first repository behavior? A: Answer by test pyramid intent: unit for logic speed, integration for boundaries, and end-to-end for critical journeys with flakiness controls.
- Q: What do you mock vs keep real? A: Answer by test pyramid intent: unit for logic speed, integration for boundaries, and end-to-end for critical journeys with flakiness controls.
Production Considerations¶
- Validate retry and conflict-resolution paths.
Performance Insights¶
- Keep integration tests narrow to maintain CI throughput.
Senior-Level Insights¶
- Strong candidates explain consistency and sync semantics, not just assertions.