0
0
Kotlinprogramming~5 mins

Mocking with MockK in Kotlin - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is MockK used for in Kotlin testing?
MockK is a library used to create mock objects in Kotlin tests. It helps simulate and control the behavior of dependencies to test code in isolation.
Click to reveal answer
beginner
How do you create a simple mock object with MockK?
Use the function mockk<ClassName>() to create a mock instance of a class. For example, val mock = mockk<MyClass>().
Click to reveal answer
beginner
What does the every { ... } returns ... syntax do in MockK?
It defines the behavior of a mocked function. Inside every { ... } you specify the function call, and returns sets what value it should return when called.
Click to reveal answer
intermediate
How do you verify that a mocked function was called in MockK?
Use the verify { ... } block with the function call inside. This checks if the function was called during the test.
Click to reveal answer
intermediate
What is the difference between mockk() and spyk() in MockK?
mockk() creates a pure mock with no real behavior, while spyk() creates a spy that wraps a real object and allows you to mock or verify some behavior while keeping the original behavior for others.
Click to reveal answer
Which MockK function creates a mock object?
Aevery()
Bspyk()
Cmockk()
Dverify()
How do you specify what a mocked function should return?
Ausing <code>spyk()</code>
Busing <code>verify { ... }</code>
Cusing <code>mockk()</code>
Dusing <code>returns</code> inside <code>every { ... }</code>
What does verify { mock.someFunction() } check?
AThat <code>someFunction()</code> was called on <code>mock</code>
BThat <code>someFunction()</code> returns a value
CThat <code>mock</code> is a spy
DThat <code>mock</code> is a real object
Which MockK function lets you keep original behavior but mock some parts?
Amockk()
Bspyk()
Cevery()
Dverify()
What is the main purpose of mocking in tests?
ATo isolate the code being tested by simulating dependencies
BTo make the code run faster
CTo write less code
DTo create user interfaces
Explain how to create a mock and define its behavior using MockK.
Think about how you tell a mock what to do when a function is called.
You got /3 concepts.
    Describe the difference between a mock and a spy in MockK and when you might use each.
    Consider if you want to keep original behavior or replace it completely.
    You got /3 concepts.