Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete 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'
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.
✗ Incorrect
Use mockk to create a mock object in MockK.
2fill in blank
mediumComplete 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using a method name that does not exist in the class.
Confusing getUserId with getUserName.
✗ Incorrect
The method to stub is getUserName as it returns the user name.
3fill in blank
hardFix 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using zero which means never called.
Using 'any' which is not a valid number.
✗ Incorrect
Use exactly = 1 to verify the method was called once.
4fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using mockk instead of spyk for spying.
Stubbing a method name that does not exist.
✗ Incorrect
Use spyk to create a spy and stub the fetchData method.
5fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong number for calls.
Not using any() matcher.
Asserting wrong user name.
✗ Incorrect
Verify called twice, with any User object, and assert the captured user's name is "Alice".