A fake is a simple implementation of a repository or data source used in tests to simulate real behavior without accessing real databases or network. It helps test logic in isolation.
Click to reveal answer
intermediate
Why use fakes instead of mocks in repository testing?
Fakes provide a working implementation with simple logic, making tests more realistic and easier to maintain compared to mocks, which only simulate behavior and require setup for each test.
Click to reveal answer
beginner
How does a fake repository improve test reliability?
By using a fake repository, tests run quickly and consistently without relying on external systems like databases or network, reducing flakiness and making debugging easier.
Click to reveal answer
intermediate
What is a key difference between a fake and a stub?
A fake has a working implementation with simple logic, while a stub only returns fixed data without any logic.
Click to reveal answer
beginner
In Kotlin, how might you implement a fake repository for testing?
Create a class implementing the repository interface with in-memory data structures to store and return data, simulating real repository behavior without external dependencies.
Click to reveal answer
What is the main purpose of using a fake repository in tests?
ATo replace UI components
BTo slow down tests for accuracy
CTo simulate real data access without external dependencies
DTo connect to a real database
✗ Incorrect
Fakes simulate data access without relying on real databases or network, making tests faster and more reliable.
Which of these is true about a fake repository?
AIt has simple logic and stores data in memory
BIt is used only for UI testing
CIt only returns fixed data without logic
DIt contains real database connections
✗ Incorrect
A fake repository uses simple logic and in-memory data to simulate real repository behavior.
Why might you prefer a fake over a mock in repository testing?
AFakes provide a working implementation making tests more realistic
BMocks are slower than fakes
CMocks require no setup
DFakes connect to real services
✗ Incorrect
Fakes have simple working logic, making tests closer to real scenarios compared to mocks which only simulate calls.
What is a common data storage method inside a fake repository?
AFile system storage
BIn-memory collections like lists or maps
CRemote server calls
DDatabase queries
✗ Incorrect
Fakes often use in-memory collections to store and retrieve data quickly during tests.
Which statement about repository testing with fakes is false?
AFakes implement repository interfaces
BFakes help isolate tests from external systems
CFakes improve test speed and reliability
DFakes always connect to real databases
✗ Incorrect
Fakes do not connect to real databases; they simulate data access in memory.
Explain how a fake repository works and why it is useful in testing Android apps.
Think about how you can test data logic without using a real database or network.
You got /5 concepts.
Describe the difference between a fake, a mock, and a stub in repository testing.
Focus on how each one behaves and what they provide in tests.
You got /5 concepts.
Practice
(1/5)
1. What is the main purpose of using a fake repository in Android Kotlin testing?
easy
A. To replace the Android framework in tests
B. To improve UI animations during testing
C. To increase app size by adding more classes
D. To simulate data operations without accessing real databases or network
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 D
Quick Check:
Fake repository = simulate data without real sources [OK]
Hint: Fakes replace real data sources to speed up tests [OK]
Common Mistakes:
Thinking fakes improve UI animations
Confusing fakes with Android framework replacement
Assuming fakes increase app size
2. Which Kotlin syntax correctly declares a fake repository class implementing an interface UserRepository?
easy
A. class FakeUserRepository extends UserRepository { }
B. class FakeUserRepository implements UserRepository { }
C. class FakeUserRepository : UserRepository { }
D. class FakeUserRepository inherits UserRepository { }
Kotlin uses a colon (:) to implement interfaces, not keywords like implements or extends.
Step 2: Match correct syntax
class FakeUserRepository : UserRepository { } uses : correctly to implement UserRepository.
Final Answer:
class FakeUserRepository : UserRepository { } -> Option C
Quick Check:
Kotlin interface implementation uses ':' [OK]
Hint: Use ':' to implement interfaces in Kotlin [OK]
Common Mistakes:
Using Java keywords like implements or extends in Kotlin
Confusing inheritance with interface implementation
Omitting the colon ':'
3. Given this fake repository code snippet, what will 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()
medium
A. 2
B. 1
C. 0
D. Compilation error
Solution
Step 1: Analyze the users list initialization
The users list contains two elements: "Alice" and "Bob".
Step 2: Understand getUserCount() behavior
The method returns the size of the users list, which is 2.
Final Answer:
2 -> Option A
Quick Check:
users.size = 2 [OK]
Hint: Count elements in the list for size [OK]
Common Mistakes:
Assuming empty list by default
Confusing method return with list content
Expecting compilation error due to interface
4. Identify the error in this fake repository implementation:
class FakeUserRepository : UserRepository {
private val users = mutableListOf<String>()
fun getUserCount() = users.size
}
medium
A. Missing override keyword for getUserCount()
B. users list should be immutable
C. Class should not implement UserRepository
D. No constructor defined
Solution
Step 1: Check interface method implementation
The interface UserRepository requires getUserCount() to be overridden.
Step 2: Verify method declaration
The method lacks the override keyword, causing a compilation error.
Final Answer:
Missing override keyword for getUserCount() -> Option A
Quick Check:
Interface methods need 'override' in Kotlin [OK]
Hint: Always use 'override' for interface methods [OK]
Common Mistakes:
Forgetting 'override' keyword
Thinking mutable list is wrong here
Assuming constructor is mandatory
5. You want to test a repository method that fetches users filtered by minimum age. Your fake repository stores users as data class User(val name: String, val age: Int). Which fake repository method correctly returns users older than or equal to the given age?
hard
A. fun getUsersByMinAge(minAge: Int) = users.filter { it.age <= minAge }
B. fun getUsersByMinAge(minAge: Int) = users.filter { it.age >= minAge }
C. fun getUsersByMinAge(minAge: Int) = users.filter { it.age > minAge }
D. fun getUsersByMinAge(minAge: Int) = users.filter { it.name.length >= minAge }
Solution
Step 1: Understand filtering condition
The requirement is to get users with age greater than or equal to minAge.
Step 2: Match correct filter predicate
fun getUsersByMinAge(minAge: Int) = users.filter { it.age >= minAge } uses it.age >= minAge, which matches the requirement exactly.
Final Answer:
fun getUsersByMinAge(minAge: Int) = users.filter { it.age >= minAge } -> Option B
Quick Check:
Filter with '>= minAge' for correct users [OK]
Hint: Use '>=' to include minimum age users [OK]
Common Mistakes:
Using '>' excludes users exactly at minAge
Using '<=' returns younger users
Filtering by name length instead of age
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.