MockK is a Kotlin library used to create mock objects for unit testing. It helps simulate behavior of real objects to test code in isolation.
Click to reveal answer
beginner
How do you create a mock object with MockK?
Use the function mockk<ClassName>() to create a mock instance of a class.
Click to reveal answer
intermediate
What does the every { ... } returns ... syntax do in MockK?
It defines the behavior of a mocked function call. When the function inside every { } is called, it returns the specified value.
Click to reveal answer
intermediate
How do you verify that a mocked function was called in MockK?
Use the verify { mock.functionName() } block to check if the function was called during the test.
Click to reveal answer
advanced
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 can call real methods unless mocked.
Click to reveal answer
Which function creates a mock object in MockK?
Amockk()
Bspy()
Cstub()
Dfake()
✗ Incorrect
The mockk() function creates a mock object in MockK.
How do you specify the return value of a mocked function in MockK?
Awhen(function()).thenReturn(value)
Bmock(function()).returns(value)
Cgiven(function()).willReturn(value)
Devery { function() } returns value
✗ Incorrect
In MockK, use every { function() } returns value to specify return values.
What does the verify { ... } block do in MockK?
AMocks a function
BChecks if a mocked function was called
CCreates a spy object
DResets mocks
✗ Incorrect
The verify { ... } block checks if a mocked function was called during the test.
Which MockK function creates a spy that calls real methods unless mocked?
Astubk()
Bmockk()
Cspyk()
Dfakek()
✗ Incorrect
spyk() creates a spy that wraps a real object and calls real methods unless mocked.
What is the main purpose of using MockK in Android testing?
ATo simulate object behavior for unit tests
BTo handle database queries
CTo manage app navigation
DTo create UI layouts
✗ Incorrect
MockK is used to simulate object behavior to isolate and test code in unit tests.
Explain how to create and use a mock object with MockK in a Kotlin unit test.
Think about how you tell the mock what to do and how to check it was used.
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 fake everything or keep some real behavior.
You got /4 concepts.
Practice
(1/5)
1. What is the main purpose of using MockK in Android Kotlin testing?
easy
A. To create fake objects that simulate real ones for testing
B. To speed up the app's UI animations
C. To generate random data for the app
D. To compile Kotlin code faster
Solution
Step 1: Understand MockK's role
MockK is a library used to create fake objects that imitate real objects in tests.
Step 2: Identify the main use
This helps test parts of the app separately without relying on real implementations.
Final Answer:
To create fake objects that simulate real ones for testing -> Option A
Quick Check:
MockK purpose = Fake objects for testing [OK]
Hint: MockK fakes objects to isolate tests quickly [OK]
Common Mistakes:
Thinking MockK speeds up UI animations
Confusing MockK with data generators
Believing MockK affects compilation speed
2. Which of the following is the correct syntax to create a mock of a class MyClass using MockK?
easy
A. val mock = MockK<MyClass>()
B. val mock = mockk<MyClass>()
C. val mock = mock<MyClass>()
D. val mock = createMock<MyClass>()
Solution
Step 1: Recall MockK mock creation syntax
MockK uses the function mockk<T>() to create mocks of type T.
Step 2: Match syntax to options
val mock = mockk<MyClass>() matches the correct syntax exactly.
Final Answer:
val mock = mockk<MyClass>() -> Option B
Quick Check:
MockK mock creation = mockk<Type>() [OK]
Hint: Use mockk<Type>() to create mocks in MockK [OK]
Common Mistakes:
Using capitalized MockK instead of mockk
Using mock() which is from other libraries
Trying createMock() which is not in MockK
3. Given the code snippet:
val mock = mockk<Calculator>()
every { mock.add(2, 3) } returns 5
val result = mock.add(2, 3)
What is the value of result?
medium
A. Throws an exception
B. 0
C. 5
D. 2
Solution
Step 1: Understand the mocking setup
The line every { mock.add(2, 3) } returns 5 tells MockK to return 5 when add(2, 3) is called.
Step 2: Check the method call
The call mock.add(2, 3) matches the mocked behavior, so it returns 5.
Final Answer:
5 -> Option C
Quick Check:
Mocked method returns set value = 5 [OK]
Hint: Mocked calls return specified values, not real logic [OK]
Common Mistakes:
Expecting real method logic to run
Assuming default 0 return for Int
Thinking mock throws error without setup
4. What is wrong with this MockK code snippet?
val mock = mockk<UserService>()
verify { mock.getUser() }
medium
A. mockk<UserService>() is invalid syntax
B. getUser() method cannot be verified
C. verify requires a return value setup
D. verify is called before any method call on mock
Solution
Step 1: Understand verify usage
The verify block checks if a method was called on the mock.
Step 2: Check method call timing
Here, verify is called before mock.getUser() is ever called, so verification fails.
Final Answer:
verify is called before any method call on mock -> Option D
Quick Check:
Verify needs method call before checking [OK]
Hint: Call method before verify to avoid errors [OK]
Common Mistakes:
Calling verify before method invocation
Thinking verify needs return setup
Believing all methods are unverifiable
5. You want to test a function that calls fetchData() on a mock and then processes the result. Which MockK feature lets you simulate fetchData() returning different values on consecutive calls?
hard
A. Using every with returnsMany
B. Using slot to capture arguments
C. Using andThen to chain return values
D. Using relaxed mocks to auto-return defaults
Solution
Step 1: Identify feature for multiple returns
MockK's returnsMany allows specifying a list of return values for consecutive calls.
Step 2: Match feature to options
Using every with returnsMany correctly pairs every with returnsMany to simulate different returns on calls.
Final Answer:
Using every with returnsMany -> Option A
Quick Check:
Multiple returns = every + returnsMany [OK]
Hint: Use returnsMany to mock multiple call results [OK]
Common Mistakes:
Confusing andThen with returnsMany
Using slot for return values instead of capturing
Relying on relaxed mocks for varied returns
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.