What if your tests could run instantly and never fail because of slow or missing data?
Why Repository testing with fakes in Android Kotlin? - Purpose & Use Cases
Imagine you want to test your app's data fetching, but it depends on a real database or a web service that is slow or sometimes unavailable.
You try to run tests, but they fail or take forever because they rely on these real parts.
Running tests with real databases or network calls is slow and flaky.
It can cause tests to fail randomly due to network issues or data changes.
This makes it hard to trust your tests and slows down your development.
Using fakes means creating simple, pretend versions of your data sources.
These fakes behave like the real ones but are fast and reliable.
This lets you test your repository logic quickly and safely without real dependencies.
val data = realDatabase.getData() assert(data.isNotEmpty())
val fakeDb = FakeDatabase() val data = fakeDb.getData() assert(data.isNotEmpty())
It enables fast, reliable tests that focus on your app's logic, not on external systems.
When building a weather app, you can fake the weather API to return fixed data, so your tests never fail due to network problems.
Manual tests with real data sources are slow and unreliable.
Fakes simulate data sources for fast and stable tests.
This improves confidence and speed in app development.
Practice
Solution
Step 1: Understand the role of a fake repository
A fake repository mimics the real repository's behavior but uses in-memory data instead of real data sources.Step 2: Identify the benefit in testing
This allows tests to run quickly and reliably without depending on databases or network calls.Final Answer:
To simulate data operations without accessing real databases or network -> Option DQuick Check:
Fake repository = simulate data without real sources [OK]
- Thinking fakes improve UI animations
- Confusing fakes with Android framework replacement
- Assuming fakes increase app size
UserRepository?Solution
Step 1: Recall Kotlin interface implementation syntax
Kotlin uses a colon (:) to implement interfaces, not keywords like implements or extends.Step 2: Match correct syntax
class FakeUserRepository : UserRepository { } uses:correctly to implementUserRepository.Final Answer:
class FakeUserRepository : UserRepository { } -> Option CQuick Check:
Kotlin interface implementation uses ':' [OK]
- Using Java keywords like implements or extends in Kotlin
- Confusing inheritance with interface implementation
- Omitting the colon ':'
println(fakeRepo.getUserCount()) output?
interface UserRepository {
fun getUserCount(): Int
}
class FakeUserRepository : UserRepository {
private val users = mutableListOf("Alice", "Bob")
override fun getUserCount() = users.size
}
val fakeRepo = FakeUserRepository()Solution
Step 1: Analyze the users list initialization
Theuserslist contains two elements: "Alice" and "Bob".Step 2: Understand getUserCount() behavior
The method returns the size of theuserslist, which is 2.Final Answer:
2 -> Option AQuick Check:
users.size = 2 [OK]
- Assuming empty list by default
- Confusing method return with list content
- Expecting compilation error due to interface
class FakeUserRepository : UserRepository {
private val users = mutableListOf<String>()
fun getUserCount() = users.size
}Solution
Step 1: Check interface method implementation
The interfaceUserRepositoryrequiresgetUserCount()to be overridden.Step 2: Verify method declaration
The method lacks theoverridekeyword, causing a compilation error.Final Answer:
Missing override keyword for getUserCount() -> Option AQuick Check:
Interface methods need 'override' in Kotlin [OK]
- Forgetting 'override' keyword
- Thinking mutable list is wrong here
- Assuming constructor is mandatory
data class User(val name: String, val age: Int). Which fake repository method correctly returns users older than or equal to the given age?Solution
Step 1: Understand filtering condition
The requirement is to get users with age greater than or equal tominAge.Step 2: Match correct filter predicate
fun getUsersByMinAge(minAge: Int) = users.filter { it.age >= minAge } usesit.age >= minAge, which matches the requirement exactly.Final Answer:
fun getUsersByMinAge(minAge: Int) = users.filter { it.age >= minAge } -> Option BQuick Check:
Filter with '>= minAge' for correct users [OK]
- Using '>' excludes users exactly at minAge
- Using '<=' returns younger users
- Filtering by name length instead of age
Ready to prove it?
Pass the test and this pattern moves unknown
