0
0
Android Kotlinmobile~10 mins

Repository testing with fakes in Android Kotlin - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to declare a fake repository class implementing the UserRepository interface.

Android Kotlin
class FakeUserRepository : UserRepository {
    override fun getUser(id: Int): User {
        return [1]
    }
}
Drag options to blanks, or click blank then click option'
AUser()
Bnull
Cthrow Exception("Not implemented")
DUser(id, "Fake User")
Attempts:
3 left
💡 Hint
Common Mistakes
Returning null instead of a User object.
Throwing an exception instead of returning a fake user.
2fill in blank
medium

Complete the test function to verify the fake repository returns the expected user name.

Android Kotlin
fun testGetUserReturnsFake() {
    val repo = FakeUserRepository()
    val user = repo.getUser(1)
    assert(user.name == [1])
}
Drag options to blanks, or click blank then click option'
A"Real User"
B"Test User"
C"Fake User"
D"Unknown"
Attempts:
3 left
💡 Hint
Common Mistakes
Using the wrong expected name in the assertion.
Confusing the fake user name with a real user name.
3fill in blank
hard

Fix the error in the fake repository to store users in a mutable map and return them by id.

Android Kotlin
class FakeUserRepository : UserRepository {
    private val users = mutableMapOf<Int, User>()

    fun addUser(user: User) {
        users[[1]] = user
    }

    override fun getUser(id: Int): User? {
        return users[[2]]
    }
}
Drag options to blanks, or click blank then click option'
Auser.id
Buser.name
Cid
Duser
Attempts:
3 left
💡 Hint
Common Mistakes
Using user.name as the key instead of user.id.
Using user instead of id to retrieve from the map.
4fill in blank
hard

Fill both blanks to complete the test that adds a user to the fake repository and verifies retrieval.

Android Kotlin
fun testAddAndGetUser() {
    val repo = FakeUserRepository()
    val user = User(10, "Test User")
    repo.[1](user)
    val retrieved = repo.[2](10)
    assert(retrieved?.name == "Test User")
}
Drag options to blanks, or click blank then click option'
AaddUser
BgetUser
CremoveUser
DupdateUser
Attempts:
3 left
💡 Hint
Common Mistakes
Using removeUser or updateUser instead of addUser or getUser.
Mixing up the order of method calls.
5fill in blank
hard

Fill all three blanks to create a fake repository with a mutable list and implement add, get, and clear methods.

Android Kotlin
class FakeUserRepository : UserRepository {
    private val users = mutableListOf<User>()

    fun [1](user: User) {
        users.[2](user)
    }

    fun [3]() {
        users.clear()
    }

    override fun getUser(id: Int): User? {
        return users.find { it.id == id }
    }
}
Drag options to blanks, or click blank then click option'
AaddUser
Badd
CclearUsers
Dclear
Attempts:
3 left
💡 Hint
Common Mistakes
Using incorrect method names that don't match the calls.
Calling wrong list methods like remove instead of add.