or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

annotations.mdindex.mdmatchers.mdmock-creation.mdspecial-mocking.mdstubbing.mdverification.md
tile.json

tessl/maven-io-mockk--mockk

Comprehensive mocking library for Kotlin with native coroutine support and advanced DSL features.

Workspace
tessl
Visibility
Public
Created
Last updated
Describes
mavenpkg:maven/io.mockk/mockk@1.14.x

To install, run

npx @tessl/cli install tessl/maven-io-mockk--mockk@1.14.0

index.mddocs/

MockK

MockK 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.

Package Information

  • Package Name: io.mockk:mockk
  • Package Type: maven
  • Language: Kotlin
  • Installation: implementation("io.mockk:mockk:1.14.3")

Core Imports

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.slot

Basic Usage

import 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()) }

Architecture

MockK is built around several key components:

  • Mock Creation: Core functions (mockk, spyk) for creating different types of test doubles
  • DSL Scopes: Fluent interfaces (MockKMatcherScope, MockKStubScope, MockKVerificationScope) for composing test scenarios
  • Stubbing Engine: Behavior definition system with extensive answer types and matchers
  • Verification System: Comprehensive call verification with ordering, counting, and timeout support
  • Platform Integration: Multiplatform support with JVM-specific features for static and constructor mocking
  • Annotation Support: Automatic mock initialization through annotations and JUnit integration

Capabilities

Mock Creation

Core 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 = {}
): T

Mock Creation

Stubbing

Behavior 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)

Stubbing

Verification

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
)

Verification

Argument Matching

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>): T

Matchers

Special Mocking

Advanced 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
)

Special Mocking

Annotations

Annotation-based mock initialization system with automatic dependency injection and JUnit integration for streamlined test setup.

@MockK
@RelaxedMockK
@SpyK
@InjectMockKs

Annotations

Core Types

interface 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<*>
}