Comprehensive mocking library for Kotlin with native coroutine support and advanced DSL features.
npx @tessl/cli install tessl/maven-io-mockk--mockk@1.14.0MockK is a comprehensive mocking library specifically designed for Kotlin that provides native support for Kotlin language features including coroutines, extension functions, data classes, and object mocks. It offers a rich DSL for creating mocks, spies, and stubs with advanced capabilities like relaxed mocking, partial mocking, verification with call ordering, argument capturing, and hierarchical mocking.
implementation("io.mockk:mockk:1.14.3")import io.mockk.*For specific functionality:
import io.mockk.mockk
import io.mockk.spyk
import io.mockk.every
import io.mockk.verify
import io.mockk.slotimport io.mockk.*
// Create a mock
interface UserService {
fun getUserById(id: String): User
fun saveUser(user: User): Boolean
}
val userService = mockk<UserService>()
// Stub behavior
every { userService.getUserById("123") } returns User("123", "John")
every { userService.saveUser(any()) } returns true
// Use the mock
val user = userService.getUserById("123")
userService.saveUser(user)
// Verify interactions
verify { userService.getUserById("123") }
verify { userService.saveUser(any()) }MockK is built around several key components:
mockk, spyk) for creating different types of test doublesMockKMatcherScope, MockKStubScope, MockKVerificationScope) for composing test scenariosCore mock and spy creation functions with comprehensive configuration options for different testing scenarios.
inline fun <reified T : Any> mockk(
name: String? = null,
relaxed: Boolean = false,
vararg moreInterfaces: KClass<*>,
relaxUnitFun: Boolean = false,
mockValidator: MockkValidator = MockkValidator(RestrictMockkConfiguration()),
block: T.() -> Unit = {}
): T
inline fun <reified T : Any> spyk(
name: String? = null,
vararg moreInterfaces: KClass<*>,
recordPrivateCalls: Boolean = false,
block: T.() -> Unit = {}
): T
inline fun <reified T : Any> spyk(
objToCopy: T,
name: String? = null,
vararg moreInterfaces: KClass<*>,
recordPrivateCalls: Boolean = false,
block: T.() -> Unit = {}
): TBehavior definition system for specifying how mocks should respond to method calls, with support for complex scenarios and coroutines.
fun <T> every(stubBlock: MockKMatcherScope.() -> T): MockKStubScope<T, T>
fun <T> coEvery(stubBlock: suspend MockKMatcherScope.() -> T): MockKStubScope<T, T>
fun justRun(stubBlock: MockKMatcherScope.() -> Unit)
fun coJustRun(stubBlock: suspend MockKMatcherScope.() -> Unit)Comprehensive call verification system with support for call ordering, counting, timeouts, and complex verification patterns.
fun verify(
ordering: Ordering = Ordering.UNORDERED,
inverse: Boolean = false,
atLeast: Int = 1,
atMost: Int = Int.MAX_VALUE,
exactly: Int = -1,
timeout: Long = 0,
verifyBlock: MockKVerificationScope.() -> Unit
)
fun coVerify(
ordering: Ordering = Ordering.UNORDERED,
inverse: Boolean = false,
atLeast: Int = 1,
atMost: Int = Int.MAX_VALUE,
exactly: Int = -1,
timeout: Long = 0,
verifyBlock: suspend MockKVerificationScope.() -> Unit
)Sophisticated argument matching system with built-in matchers for equality, comparison, type checking, capturing, and logical operations.
// In MockKMatcherScope context
fun <T> any(): T
fun <T> eq(value: T): T
fun <T> capture(slot: CapturingSlot<T>): T
fun <T> match(matcher: Matcher<T>): TAdvanced mocking capabilities for static methods, object instances, and constructors, enabling testing of complex Kotlin constructs.
inline fun mockkStatic(vararg classes: KClass<*>)
inline fun mockkObject(vararg objects: Any, recordPrivateCalls: Boolean = false)
inline fun mockkConstructor(
vararg classes: KClass<*>,
recordPrivateCalls: Boolean = false,
localToThread: Boolean = false
)Annotation-based mock initialization system with automatic dependency injection and JUnit integration for streamlined test setup.
@MockK
@RelaxedMockK
@SpyK
@InjectMockKsinterface Matcher<in T> {
fun match(arg: T?): Boolean
}
class CapturingSlot<T> {
val captured: T
val isCaptured: Boolean
fun clear()
}
interface Answer<out T> {
fun answer(call: Call): T
suspend fun coAnswer(call: Call): T
}
enum class Ordering {
UNORDERED, ALL, ORDERED, SEQUENCE
}
class MockkValidator(configuration: RestrictMockkConfiguration)
class RestrictMockkConfiguration
interface Call {
val invocation: Invocation
val matcher: InvocationMatcher
}
interface Invocation {
val self: Any
val method: MethodDescription
val args: List<Any?>
}
interface MethodDescription {
val name: String
val returnType: kotlin.reflect.KClass<*>
val declaringClass: kotlin.reflect.KClass<*>
}