Using fakes in repository testing does not affect your app's runtime performance or battery life because these tests run separately from the app on your development machine. However, well-designed fakes help catch bugs early, preventing performance issues in the released app.
Repository testing with fakes in Android Kotlin - Build, Publish & Deploy
Keep your fake implementations simple and focused on the data your tests need. Avoid complex logic or heavy operations in fakes to keep tests fast and reliable. Fast tests encourage frequent runs, improving code quality without slowing development.
Fakes are only used in testing and are not included in your app's release build. Therefore, they have no impact on your app's bundle size or startup time.
On Android with Kotlin, repository testing with fakes is commonly done using JUnit and Kotlin classes. On iOS, similar testing uses Swift and XCTest with protocol-based fakes. Both platforms benefit from isolating data sources during tests, but the tools and languages differ.
Repository testing with fakes is a development practice and does not affect app store submissions directly. However, thorough testing helps ensure your app meets store quality guidelines by reducing crashes and bugs.
Your app takes 5 seconds to load this screen. What's likely wrong?
- The repository might be making real network or database calls during tests instead of using fakes, causing delays.
- Tests or app code may be blocking the main thread with heavy operations.
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
