0
0
Android Kotlinmobile~10 mins

MockK for mocking 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 create a mock of the UserRepository class using MockK.

Android Kotlin
val userRepository = [1]<UserRepository>()
Drag options to blanks, or click blank then click option'
Amockk
Bspyk
Cevery
Dverify
Attempts:
3 left
💡 Hint
Common Mistakes
Using spyk instead of mockk when a pure mock is needed.
Trying to use every or verify to create a mock.
2fill in blank
medium

Complete the code to stub the getUserName() method to return "Alice".

Android Kotlin
every { userRepository.[1]() } returns "Alice"
Drag options to blanks, or click blank then click option'
AgetUserId
BgetUserName
CfetchUser
DloadName
Attempts:
3 left
💡 Hint
Common Mistakes
Using a method name that does not exist in the class.
Confusing getUserId with getUserName.
3fill in blank
hard

Fix the error in verifying that getUserName() was called exactly once.

Android Kotlin
verify(exactly = [1]) { userRepository.getUserName() }
Drag options to blanks, or click blank then click option'
A1
B2
Cany
D0
Attempts:
3 left
💡 Hint
Common Mistakes
Using zero which means never called.
Using 'any' which is not a valid number.
4fill in blank
hard

Fill both blanks to create a spy of UserService and stub fetchData() to return "Data".

Android Kotlin
val userService = [1]<UserService>()
every { userService.[2]() } returns "Data"
Drag options to blanks, or click blank then click option'
Aspyk
Bmockk
CfetchData
DgetData
Attempts:
3 left
💡 Hint
Common Mistakes
Using mockk instead of spyk for spying.
Stubbing a method name that does not exist.
5fill in blank
hard

Fill all three blanks to verify that saveUser() was called with any User object exactly twice.

Android Kotlin
verify(exactly = [1]) { userRepository.saveUser([2]) }

val slot = slot<User>()
verify { userRepository.saveUser(capture(slot)) }

assert(slot.captured.name == [3])
Drag options to blanks, or click blank then click option'
A2
Bany()
C"Alice"
D"Bob"
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong number for calls.
Not using any() matcher.
Asserting wrong user name.