We test repositories with fakes to check if our data handling works correctly without using real databases or network calls.
Repository testing with fakes in Android Kotlin
interface UserRepository {
suspend fun getUser(id: String): User?
}
class FakeUserRepository : UserRepository {
private val users = mutableMapOf<String, User>()
fun addUser(user: User) {
users[user.id] = user
}
override suspend fun getUser(id: String): User? {
return users[id]
}
}A FakeUserRepository implements the same interface as the real repository.
It stores data in memory for quick testing without real data sources.
interface ProductRepository {
suspend fun getProduct(id: String): Product?
}
class FakeProductRepository : ProductRepository {
private val products = mutableMapOf<String, Product>()
fun addProduct(product: Product) {
products[product.id] = product
}
override suspend fun getProduct(id: String): Product? {
return products[id]
}
}class FakeUserRepository : UserRepository { private val users = listOf(User("1", "Alice"), User("2", "Bob")) override suspend fun getUser(id: String): User? { return users.find { it.id == id } } }
This example shows a fake user repository storing users in memory. The test function adds users and fetches one by ID, printing the name.
data class User(val id: String, val name: String) interface UserRepository { suspend fun getUser(id: String): User? } class FakeUserRepository : UserRepository { private val users = mutableMapOf<String, User>() fun addUser(user: User) { users[user.id] = user } override suspend fun getUser(id: String): User? { return users[id] } } suspend fun testFakeRepository() { val fakeRepo = FakeUserRepository() fakeRepo.addUser(User("1", "Alice")) fakeRepo.addUser(User("2", "Bob")) val user = fakeRepo.getUser("1") println(user?.name ?: "User not found") } // To run testFakeRepository, use a coroutine scope or runBlocking in main
Fakes are simple and fast but do not test real database or network behavior.
Use fakes to isolate repository logic from external dependencies.
Remember to keep fake data realistic for better test coverage.
Repository testing with fakes helps test data logic without real data sources.
Fakes implement the same interface as real repositories but use in-memory data.
This approach makes tests faster and more reliable by avoiding external dependencies.
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
