0
0
Kotlinprogramming~10 mins

Mocking with MockK in Kotlin - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Mocking with MockK
Create Mock Object
Define Behavior with every {}
Call Mocked Method
Verify Calls with verify {}
Assert Results
End
This flow shows how to create a mock, define its behavior, call it, verify interactions, and assert results using MockK.
Execution Sample
Kotlin
val mockList = mockk<MutableList<String>>()
every { mockList.size } returns 3
println(mockList.size)
verify { mockList.size }
This code mocks a MutableList, sets size to return 3, prints size, and verifies the size property was accessed.
Execution Table
StepActionEvaluationResult
1Create mock object mockListmockList is a mock of MutableList<String>mockList created
2Define behavior: every { mockList.size } returns 3When mockList.size is called, return 3Behavior set
3Call mockList.sizemockList.size calledReturns 3
4Print mockList.sizePrints 3Output: 3
5Verify mockList.size was calledCheck if mockList.size was accessedVerification passed
6End of testNo more actionsTest complete
💡 All steps executed; mock behavior defined and verified successfully.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4Final
mockListnullmock objectmock object with size=3 behaviormock object with size=3 behaviormock object with size=3 behaviormock object with size=3 behavior
mockList.sizeN/AN/Areturns 3333
Key Moments - 3 Insights
Why does mockList.size return 3 even though the list is empty?
Because we defined the behavior with every { mockList.size } returns 3 in step 2, so the mock returns 3 regardless of actual data.
What happens if we call a method on mockList without defining behavior?
MockK will throw an exception or return default values unless behavior is defined; here, size was defined to return 3 (see step 2).
Why do we use verify { mockList.size } after calling it?
To check that the mocked method was actually called during the test, ensuring interaction correctness (step 5).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what value does mockList.size return at step 3?
A3
B0
Cnull
DThrows exception
💡 Hint
Check the 'Result' column at step 3 in the execution_table.
At which step is the behavior of mockList.size defined?
AStep 1
BStep 2
CStep 4
DStep 5
💡 Hint
Look at the 'Action' column describing 'Define behavior' in the execution_table.
If we remove the verify { mockList.size } call, what changes in the execution?
AThe mockList.size would not return 3
BThe mockList would not be created
CThe test would not check if size was called
DThe println would print null
💡 Hint
Refer to step 5 in the execution_table about verification.
Concept Snapshot
Mocking with MockK:
- Create mock with mockk<T>()
- Define behavior with every { ... } returns ...
- Call mocked methods normally
- Verify calls with verify { ... }
- Use to isolate and test code parts
Full Transcript
This example shows how to use MockK in Kotlin to create a mock object of MutableList<String>. We define that when the size property is accessed, it returns 3. Then we call mockList.size and print the result, which outputs 3. Finally, we verify that the size property was accessed during the test. This helps test code that depends on list size without needing a real list.