Bird
Raised Fist0
Android Kotlinmobile~20 mins

Repository testing with fakes in Android Kotlin - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Repository Testing Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
1:30remaining
Understanding the role of a fake repository in testing

In Android development, why do we use a fake repository when writing tests for our app?

ATo automatically generate user interface layouts for different screen sizes.
BTo replace the UI components with mock views during testing.
CTo simulate data sources without relying on real databases or network calls, making tests faster and more reliable.
DTo encrypt data before saving it to the device storage.
Attempts:
2 left
💡 Hint

Think about why tests should not depend on slow or unreliable external systems.

❓ ui_behavior
intermediate
1:30remaining
Effect of fake repository on UI test behavior

When using a fake repository in UI tests, what is the expected behavior of the app's UI compared to using a real repository?

AThe UI shows consistent, predictable data quickly without delays caused by network or database access.
BThe UI randomly changes data to simulate network errors.
CThe UI disables all buttons to prevent user interaction during tests.
DThe UI automatically logs out the user after each test.
Attempts:
2 left
💡 Hint

Consider how fake data affects loading times and data consistency.

❓ lifecycle
advanced
2:00remaining
Lifecycle impact of fake repository in ViewModel tests

In a ViewModel test using a fake repository, what lifecycle aspect must you consider to ensure LiveData updates are observed correctly?

AYou must run the test on the main UI thread only.
BYou need to manually call <code>onStart()</code> and <code>onStop()</code> on the ViewModel.
CYou should disable the ViewModel lifecycle to prevent updates.
DYou must observe LiveData on a <code>TestCoroutineDispatcher</code> or use <code>InstantTaskExecutorRule</code> to execute tasks synchronously.
Attempts:
2 left
💡 Hint

Think about how LiveData posts updates and how tests can observe them immediately.

📝 Syntax
advanced
2:00remaining
Correct fake repository implementation snippet

Which Kotlin code snippet correctly implements a simple fake repository that returns a fixed list of strings?

Android Kotlin
interface DataRepository {
  suspend fun fetchItems(): List<String>
}

class FakeDataRepository : DataRepository {
  override suspend fun fetchItems(): List<String> {
    return listOf("apple", "banana", "cherry")
  }
}
A
class FakeDataRepository : DataRepository {
  override suspend fun fetchItems(): List&lt;String&gt; {
    return listOf("apple", "banana", "cherry")
  }
}
B
class FakeDataRepository : DataRepository {
  override fun fetchItems(): List&lt;String&gt; {
    return listOf("apple", "banana", "cherry")
  }
}
C
class FakeDataRepository : DataRepository {
  fun fetchItems(): List&lt;String&gt; {
    return listOf("apple", "banana", "cherry")
  }
}
D
class FakeDataRepository : DataRepository {
  override suspend fun fetchItems(): String {
    return "apple, banana, cherry"
  }
}
Attempts:
2 left
💡 Hint

Check the function signature matches the interface exactly, including suspend and return type.

🔧 Debug
expert
2:30remaining
Identifying test failure cause with fake repository

You wrote a test using a fake repository, but the test fails because the ViewModel's LiveData never updates. What is the most likely cause?

AThe fake repository returns an empty list instead of null.
BThe test does not use <code>InstantTaskExecutorRule</code>, so LiveData updates are not observed synchronously.
CThe ViewModel is not initialized before the test runs.
DThe fake repository uses real network calls instead of fake data.
Attempts:
2 left
💡 Hint

Think about how LiveData updates are delivered in tests and what rules help with that.

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

  1. 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.
  2. Step 2: Identify the benefit in testing

    This allows tests to run quickly and reliably without depending on databases or network calls.
  3. Final Answer:

    To simulate data operations without accessing real databases or network -> Option D
  4. 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 { }

Solution

  1. Step 1: Recall Kotlin interface implementation syntax

    Kotlin uses a colon (:) to implement interfaces, not keywords like implements or extends.
  2. Step 2: Match correct syntax

    class FakeUserRepository : UserRepository { } uses : correctly to implement UserRepository.
  3. Final Answer:

    class FakeUserRepository : UserRepository { } -> Option C
  4. 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

  1. Step 1: Analyze the users list initialization

    The users list contains two elements: "Alice" and "Bob".
  2. Step 2: Understand getUserCount() behavior

    The method returns the size of the users list, which is 2.
  3. Final Answer:

    2 -> Option A
  4. 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

  1. Step 1: Check interface method implementation

    The interface UserRepository requires getUserCount() to be overridden.
  2. Step 2: Verify method declaration

    The method lacks the override keyword, causing a compilation error.
  3. Final Answer:

    Missing override keyword for getUserCount() -> Option A
  4. 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

  1. Step 1: Understand filtering condition

    The requirement is to get users with age greater than or equal to minAge.
  2. Step 2: Match correct filter predicate

    fun getUsersByMinAge(minAge: Int) = users.filter { it.age >= minAge } uses it.age >= minAge, which matches the requirement exactly.
  3. Final Answer:

    fun getUsersByMinAge(minAge: Int) = users.filter { it.age >= minAge } -> Option B
  4. 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 unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard