0
0
Kotlinprogramming~10 mins

Mocking with MockK in 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 Calculator class using MockK.

Kotlin
val calculator = [1]<Calculator>()
Drag options to blanks, or click blank then click option'
Amock
Bmockk
Cspyk
Dfakek
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'spyk' instead of 'mockk' creates a spy, not a mock.
Using 'mock' or 'fakek' are not valid MockK functions.
2fill in blank
medium

Complete the code to stub the add method to return 10 when called with any two integers.

Kotlin
every { calculator.add([1], [1]) } returns 10
Drag options to blanks, or click blank then click option'
AanyNumber()
BanyInt()
CanyValue()
Dany()
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'anyInt()' is not a valid MockK matcher.
Using 'anyValue()' or 'anyNumber()' do not exist in MockK.
3fill in blank
hard

Fix the error in verifying that the subtract method was called exactly once with arguments 5 and 3.

Kotlin
verify(exactly = [1]) { calculator.subtract(5, 3) }
Drag options to blanks, or click blank then click option'
A1
B2
C0
D-1
Attempts:
3 left
💡 Hint
Common Mistakes
Using 0 means the method was never called.
Using 2 means verifying two calls, which is incorrect here.
4fill in blank
hard

Fill both blanks to create a spy of the Service class and stub its fetchData method to return "data".

Kotlin
val service = [1]<Service>()
every { service.[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' creates a mock, not a spy.
Using 'getData' is not the correct method name.
5fill in blank
hard

Fill all three blanks to verify that the save method was called at least twice with any string argument.

Kotlin
verify(atLeast = [1]) { repository.[2]([3]) }
Drag options to blanks, or click blank then click option'
A1
Bsave
Cany()
D2
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'atLeast = 1' checks for at least one call, not two.
Using wrong method name or argument matcher causes verification failure.