MockK helps you test your Android app by pretending parts of your code work a certain way. This way, you can check if your app behaves correctly without needing everything to be ready.
MockK for mocking in Android Kotlin
val mockObject = mockk<ClassName>()
every { mockObject.someMethod() } returns someValue
verify { mockObject.someMethod() }mockk() creates a fake version of a class.
every { ... } returns ... sets what the fake should return when a method is called.
verify { ... } checks if a method was called during the test.
val mockList = mockk<MutableList<String>>()
every { mockList.size } returns 3val mockService = mockk<Service>()
every { mockService.getData() } returns "Hello"verify { mockService.getData() }This code creates a fake Calculator. It tells the fake to return 10 when add(2, 3) is called. Then it calls add(2, 3) and prints the result. Finally, it checks if add(2, 3) was called.
import io.mockk.mockk import io.mockk.every import io.mockk.verify class Calculator { fun add(a: Int, b: Int): Int = a + b } fun main() { val mockCalc = mockk<Calculator>() every { mockCalc.add(2, 3) } returns 10 val result = mockCalc.add(2, 3) println("Result: $result") verify { mockCalc.add(2, 3) } }
MockK works well with Kotlin and supports many features like spying and relaxed mocks.
Always verify important calls to make sure your code behaves as expected.
Use relaxed mocks if you want default values without specifying every behavior.
MockK creates fake objects to help test parts of your app separately.
You can tell fakes what to return and check if methods were called.
This makes testing easier, faster, and more reliable.
Practice
MockK in Android Kotlin testing?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 AQuick Check:
MockK purpose = Fake objects for testing [OK]
- Thinking MockK speeds up UI animations
- Confusing MockK with data generators
- Believing MockK affects compilation speed
MyClass using MockK?Solution
Step 1: Recall MockK mock creation syntax
MockK uses the functionmockk<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 BQuick Check:
MockK mock creation = mockk<Type>() [OK]
- Using capitalized MockK instead of mockk
- Using mock() which is from other libraries
- Trying createMock() which is not in MockK
val mock = mockk<Calculator>()
every { mock.add(2, 3) } returns 5
val result = mock.add(2, 3)What is the value of
result?Solution
Step 1: Understand the mocking setup
The lineevery { mock.add(2, 3) } returns 5tells MockK to return 5 whenadd(2, 3)is called.Step 2: Check the method call
The callmock.add(2, 3)matches the mocked behavior, so it returns 5.Final Answer:
5 -> Option CQuick Check:
Mocked method returns set value = 5 [OK]
- Expecting real method logic to run
- Assuming default 0 return for Int
- Thinking mock throws error without setup
val mock = mockk<UserService>()
verify { mock.getUser() }Solution
Step 1: Understand verify usage
Theverifyblock checks if a method was called on the mock.Step 2: Check method call timing
Here,verifyis called beforemock.getUser()is ever called, so verification fails.Final Answer:
verify is called before any method call on mock -> Option DQuick Check:
Verify needs method call before checking [OK]
- Calling verify before method invocation
- Thinking verify needs return setup
- Believing all methods are unverifiable
fetchData() on a mock and then processes the result. Which MockK feature lets you simulate fetchData() returning different values on consecutive calls?Solution
Step 1: Identify feature for multiple returns
MockK'sreturnsManyallows specifying a list of return values for consecutive calls.Step 2: Match feature to options
UsingeverywithreturnsManycorrectly pairseverywithreturnsManyto simulate different returns on calls.Final Answer:
Using every with returnsMany -> Option AQuick Check:
Multiple returns = every + returnsMany [OK]
- Confusing andThen with returnsMany
- Using slot for return values instead of capturing
- Relying on relaxed mocks for varied returns
Ready to prove it?
Pass the test and this pattern moves unknown
