0
0
Android Kotlinmobile~20 mins

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

Choose your learning style9 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.