Challenge - 5 Problems
MockK Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ ui_behavior
intermediate2: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?
Attempts:
2 left
💡 Hint
Think about what MockK returns when no behavior is defined.
✗ Incorrect
By default, MockK returns default values for functions when no behavior is specified. For example, Int returns 0, Boolean returns false, and objects return null.
📝 Syntax
intermediate2: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" }
Attempts:
2 left
💡 Hint
Look for the MockK function that sets behavior on mocks.
✗ Incorrect
In MockK, 'every { ... } returns ...' is the syntax to define behavior for a mocked function.
❓ lifecycle
advanced2: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?
Attempts:
2 left
💡 Hint
Think about resource cleanup and test isolation.
✗ Incorrect
Not calling unmockkAll() can cause mocks to persist and interfere with other tests, breaking test isolation.
🔧 Debug
advanced2: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)
Attempts:
2 left
💡 Hint
Consider MockK's relaxed mocks feature.
✗ Incorrect
By default, MockK mocks are strict and throw an exception if no behavior is defined. Using relaxed = true allows default values to be returned.
🧠 Conceptual
expert2: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?
Attempts:
2 left
💡 Hint
Check the correct parameter name for specifying call count in verify.
✗ Incorrect
In MockK, 'verify(exactly = 2) { ... }' checks that the function was called exactly two times with the specified arguments.