0
0
Android Kotlinmobile~20 mins

MockK for mocking in Android Kotlin - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
MockK Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
ui_behavior
intermediate
2:00remaining
MockK: What happens when you mock a class without specifying behavior?
Consider you mock a Kotlin class using MockK but do not specify any behavior for its functions. What will happen when you call a function on this mock?
AThe function returns a random value.
BThe function returns the default value for its return type (e.g., 0 for Int, null for objects).
CThe function executes the real implementation of the class.
DThe function throws a NullPointerException immediately.
Attempts:
2 left
💡 Hint
Think about what MockK returns when no behavior is defined.
📝 Syntax
intermediate
2:00remaining
MockK syntax: How to mock a function to return a specific value?
Which of the following MockK code snippets correctly mocks a function 'getData()' to return the string "Hello"?
Android Kotlin
class MyClass {
  fun getData(): String = "Real Data"
}
Aevery { myClass.getData() } returns "Hello"
Bmockk { getData() returns "Hello" }
Cwhen(myClass.getData()).thenReturn("Hello")
Dstub { myClass.getData() returns "Hello" }
Attempts:
2 left
💡 Hint
Look for the MockK function that sets behavior on mocks.
lifecycle
advanced
2:00remaining
MockK: What happens if you forget to unmockkAll() after tests?
In a test suite using MockK, what is the likely effect of not calling unmockkAll() after tests?
AThe app crashes immediately after tests.
BTests run faster because mocks are reused.
CMocks may leak and affect other tests, causing unexpected behavior.
DThere is no effect; MockK cleans up automatically.
Attempts:
2 left
💡 Hint
Think about resource cleanup and test isolation.
🔧 Debug
advanced
2:00remaining
MockK: Why does this test fail with 'no answer found' error?
Given the code snippet below, why does the test fail with a 'no answer found' error when calling myClass.getData(42)? class MyClass { fun getData(id: Int): String = "Real Data" } val myClass = mockk() every { myClass.getData(1) } returns "Data1" // Behavior defined only for specific argument val result = myClass.getData(42)
ABecause the mockk function was called incorrectly.
BBecause getData() is a final function and cannot be mocked without extra setup.
CBecause getData() is not mocked and returns Unit by default, causing a type mismatch.
DBecause the mock was not created with relaxed = true, so no default answer is provided.
Attempts:
2 left
💡 Hint
Consider MockK's relaxed mocks feature.
🧠 Conceptual
expert
2:00remaining
MockK: How to verify a function was called exactly twice with specific arguments?
Using MockK, which code snippet correctly verifies that the function 'sendMessage("Hi")' was called exactly two times?
Averify(exactly = 2) { myClass.sendMessage("Hi") }
Bverify(times = 2) { myClass.sendMessage("Hi") }
Cverify { myClass.sendMessage("Hi") twice }
Dverify(exactly = 2) { myClass.sendMessage(any()) }
Attempts:
2 left
💡 Hint
Check the correct parameter name for specifying call count in verify.