0
0
Kotlinprogramming~20 mins

Mocking with MockK in 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!
Predict Output
intermediate
2:00remaining
What is the output of this MockK verification?

Consider the following Kotlin code using MockK. What will be printed?

Kotlin
import io.mockk.mockk
import io.mockk.verify

class Calculator {
    fun add(a: Int, b: Int): Int = a + b
}

fun main() {
    val calc = mockk<Calculator>(relaxed = true)
    calc.add(2, 3)
    verify { calc.add(2, 3) }
    println("Verification passed")
}
ACompilation error: missing import
BVerification passed
Cio.mockk.MockKException: no calls found for: Calculator#add(2, 3)
DRuntime error: NullPointerException
Attempts:
2 left
💡 Hint

Think about what relaxed = true means for the mock.

Predict Output
intermediate
2:00remaining
What happens when verifying a call that did not happen?

What error will this code produce?

Kotlin
import io.mockk.mockk
import io.mockk.verify

class Service {
    fun fetchData(): String = "data"
}

fun main() {
    val service = mockk<Service>()
    verify { service.fetchData() }
}
Aio.mockk.MockKException: no calls found for: Service#fetchData()
BCompilation error: verify requires a block
CNo output, verification passes silently
DRuntime error: NullPointerException
Attempts:
2 left
💡 Hint

What happens if you verify a call that was never made?

🔧 Debug
advanced
2:00remaining
Why does this MockK stub not work as expected?

Look at this Kotlin code snippet. The stub is supposed to return 10 when compute() is called, but it returns 0 instead. Why?

Kotlin
import io.mockk.mockk
import io.mockk.every

class Processor {
    fun compute(): Int = 5
}

fun main() {
    val processor = mockk<Processor>()
    every { processor.compute() } returns 10
    println(processor.compute())
}
AThe mock is not relaxed, so it returns default Int value 0 instead of 10
BThe stub is correct; the output will be 10
CThe mockk library is not imported correctly, causing default return
DThe stub is ignored because compute() is final and cannot be mocked
Attempts:
2 left
💡 Hint

Consider Kotlin's default behavior for functions and MockK's handling of final methods.

📝 Syntax
advanced
2:00remaining
Which option correctly mocks a suspend function with MockK?

Given a suspend function suspend fun fetch(): String, which code correctly mocks it to return "hello"?

Aevery { runBlocking { mock.fetch() } } returns "hello"
BcoVerify { mock.fetch() } returns "hello"
CcoEvery { mock.fetch() } returns "hello"
Devery { mock.fetch() } returns "hello"
Attempts:
2 left
💡 Hint

MockK has special functions for suspend functions.

🚀 Application
expert
2:00remaining
How many times was the function called?

Given this code, how many times was service.process() called?

Kotlin
import io.mockk.mockk
import io.mockk.verify

class Service {
    fun process() {}
}

fun main() {
    val service = mockk<Service>(relaxed = true)
    service.process()
    service.process()
    service.process()
    verify(exactly = 2) { service.process() }
}
A3 times, but verification expects 2, so it throws an exception
B2 times, verification passes
C1 time, verification fails
D0 times, verification fails
Attempts:
2 left
💡 Hint

Count the calls and compare with the verification count.