CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/maven-io-mockk--mockk

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

Pending
Overview
Eval results
Files

verification.mddocs/

Verification

Comprehensive call verification system with support for call ordering, counting, timeouts, and complex verification patterns to ensure mocks are used as expected.

Capabilities

Basic Verification

Core verification functions for confirming mock interactions.

/**
 * Verifies that calls were made in the past
 * @param ordering How the verification should be ordered (default: UNORDERED)
 * @param inverse When true, verifies that calls did NOT happen
 * @param atLeast Minimum number of times the call should have occurred
 * @param atMost Maximum number of times the call should have occurred
 * @param exactly Exact number of times (-1 to disable)
 * @param timeout Timeout in milliseconds for verification
 * @param verifyBlock Block containing calls to verify
 */
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
)

/**
 * Coroutine version of verify for suspend functions
 */
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
)

Usage Examples:

val userService = mockk<UserService>(relaxed = true)

// Call the mock
userService.getUserById("123")
userService.saveUser(User("123", "John"))

// Basic verification
verify { userService.getUserById("123") }
verify { userService.saveUser(any()) }

// Verify with call count
verify(exactly = 1) { userService.getUserById("123") }
verify(atLeast = 1) { userService.saveUser(any()) }
verify(atMost = 2) { userService.getUserById(any()) }

// Verify coroutine calls
coVerify { userService.getUserByIdAsync("123") }

Verification Variants

Specialized verification functions for different scenarios.

/**
 * Verifies all specified calls happened (no specific order required)
 * @param inverse When true, verifies calls did NOT happen
 * @param verifyBlock Block containing all calls that should be verified
 */
fun verifyAll(
    inverse: Boolean = false,
    verifyBlock: MockKVerificationScope.() -> Unit
)

/**
 * Verifies calls happened in the declared order
 * @param inverse When true, verifies calls did NOT happen in order
 * @param verifyBlock Block containing calls in expected order
 */
fun verifyOrder(
    inverse: Boolean = false,
    verifyBlock: MockKVerificationScope.() -> Unit
)

/**
 * Verifies calls happened in exact sequence with no other calls
 * @param inverse When true, verifies sequence did NOT happen
 * @param verifyBlock Block containing exact call sequence
 */
fun verifySequence(
    inverse: Boolean = false,
    verifyBlock: MockKVerificationScope.() -> Unit
)

/**
 * Verifies specific call counts and patterns
 * @param verifyBlock Block for count-based verification
 */
fun verifyCount(verifyBlock: MockKCallCountVerificationScope.() -> Unit)

Usage Examples:

val userService = mockk<UserService>(relaxed = true)

// Make some calls
userService.getUserById("123")
userService.saveUser(User("123", "John"))
userService.deleteUser("456")

// Verify all calls happened (any order)
verifyAll {
    userService.getUserById("123")
    userService.saveUser(any())
    userService.deleteUser("456")
}

// Verify calls happened in specific order
verifyOrder {
    userService.getUserById("123")
    userService.saveUser(any())
    userService.deleteUser("456")
}

// Verify exact sequence (no other calls allowed)
verifySequence {
    userService.getUserById("123")
    userService.saveUser(any())
    userService.deleteUser("456")
}

// Count-based verification
verifyCount {
    verify(exactly = 1) { userService.getUserById("123") }
    verify(exactly = 1) { userService.saveUser(any()) }
}

Coroutine Verification

Verification functions specifically for suspend functions.

/**
 * Coroutine versions of verification functions
 */
fun coVerifyAll(
    inverse: Boolean = false,
    verifyBlock: suspend MockKVerificationScope.() -> Unit
)

fun coVerifyOrder(
    inverse: Boolean = false,
    verifyBlock: suspend MockKVerificationScope.() -> Unit
)

fun coVerifySequence(
    inverse: Boolean = false,
    verifyBlock: suspend MockKVerificationScope.() -> Unit
)

fun coVerifyCount(verifyBlock: MockKCallCountCoVerificationScope.() -> Unit)

Usage Examples:

val userService = mockk<UserService>()

// Stub suspend functions
coEvery { userService.getUserByIdAsync("123") } returns User("123", "John")
coJustRun { userService.saveUserAsync(any()) }

// Call suspend functions
runBlocking {
    userService.getUserByIdAsync("123")
    userService.saveUserAsync(User("123", "John"))
}

// Verify suspend function calls
coVerifyAll {
    userService.getUserByIdAsync("123")
    userService.saveUserAsync(any())
}

coVerifyOrder {
    userService.getUserByIdAsync("123")
    userService.saveUserAsync(any())
}

Timeout Verification

Verification with timeout support for concurrent scenarios.

/**
 * Verification with timeout - waits until verification passes or timeout
 * @param timeout Timeout in milliseconds
 */
fun verify(timeout: Long, verifyBlock: MockKVerificationScope.() -> Unit)

Usage Examples:

val userService = mockk<UserService>(relaxed = true)

// Start async operation that will call the mock
thread {
    Thread.sleep(500)
    userService.saveUser(User("123", "John"))
}

// Verify with timeout (waits up to 1000ms)
verify(timeout = 1000) { userService.saveUser(any()) }

Inverse Verification

Verifying that calls did NOT happen.

/**
 * Verify calls did NOT happen using inverse parameter
 */
fun verify(inverse: Boolean = true, verifyBlock: MockKVerificationScope.() -> Unit)

Usage Examples:

val userService = mockk<UserService>(relaxed = true)

userService.getUserById("123")
// Don't call deleteUser

// Verify call happened
verify { userService.getUserById("123") }

// Verify call did NOT happen
verify(inverse = true) { userService.deleteUser(any()) }

Was Not Called Verification

Specialized verification for confirming no calls were made.

/**
 * Verifies that a mock was not called at all
 */
val MockKVerificationScope.wasNot: WasNotCalled
object Called
infix fun WasNotCalled.called(called: Called)

Usage Examples:

val userService = mockk<UserService>()

// Don't call the mock

// Verify no calls were made
verify { userService wasNot Called }

Argument Verification

Verifying and asserting on captured arguments.

abstract class MockKVerificationScope : MockKMatcherScope {
    /**
     * Verify argument with custom assertion
     * @param captureBlock Block for asserting on the captured argument
     */
    fun <T> withArg(captureBlock: MockKAssertScope.(T) -> Unit): T
    
    fun <T> withNullableArg(captureBlock: MockKAssertScope.(T?) -> Unit): T?
    
    fun <T> coWithArg(captureBlock: suspend MockKAssertScope.(T) -> Unit): T
    
    fun <T> coWithNullableArg(captureBlock: suspend MockKAssertScope.(T?) -> Unit): T?
}

abstract class MockKAssertScope {
    /**
     * Assert equality with automatic message generation
     */
    fun checkEquals(expected: Any?)
    
    /**
     * Assert equality with custom message
     */
    fun checkEquals(msg: String, expected: Any?)
}

Usage Examples:

val userService = mockk<UserService>(relaxed = true)

// Call with specific user
userService.saveUser(User("123", "John Doe"))

// Verify with argument assertion
verify {
    userService.saveUser(
        withArg { user ->
            checkEquals("123", user.id)
            checkEquals("User name should be John Doe", "John Doe", user.name)
        }
    )
}

// Nullable argument verification
verify {
    userService.processNullableUser(
        withNullableArg { user ->
            if (user != null) {
                checkEquals("123", user.id)
            }
        }
    )
}

Verification Utilities

Additional utilities for comprehensive verification.

/**
 * Confirms that all recorded calls have been verified
 * @param mocks Mocks to check for unverified calls
 */
fun confirmVerified(vararg mocks: Any)

/**
 * Checks if all recorded calls are necessary (not over-stubbed)
 * @param mocks Mocks to check for unnecessary stubs
 */
fun checkUnnecessaryStub(vararg mocks: Any)

Usage Examples:

val userService = mockk<UserService>()
val orderService = mockk<OrderService>()

// Stub and call mocks
every { userService.getUserById("123") } returns User("123", "John")
every { orderService.getOrdersFor("123") } returns emptyList()

userService.getUserById("123")
orderService.getOrdersFor("123")

// Verify all calls
verify { userService.getUserById("123") }
verify { orderService.getOrdersFor("123") }

// Confirm all recorded calls were verified
confirmVerified(userService, orderService)

// Check for unnecessary stubs
checkUnnecessaryStub(userService, orderService)

Ordering Types

Enumeration defining different verification ordering modes.

enum class Ordering {
    /** No specific order required (default) */
    UNORDERED,
    
    /** All specified calls must match */
    ALL,
    
    /** Calls must be in the specified order */
    ORDERED,
    
    /** Exact sequence match with no other calls */
    SEQUENCE
}

Usage Examples:

val userService = mockk<UserService>(relaxed = true)

// Make ordered calls
userService.startTransaction()
userService.saveUser(User("123", "John"))
userService.commitTransaction()

// Verify with different ordering modes
verify(ordering = Ordering.ORDERED) {
    userService.startTransaction()
    userService.saveUser(any())
    userService.commitTransaction()
}

verify(ordering = Ordering.SEQUENCE) {
    userService.startTransaction()
    userService.saveUser(any())
    userService.commitTransaction()
}

Install with Tessl CLI

npx tessl i tessl/maven-io-mockk--mockk

docs

annotations.md

index.md

matchers.md

mock-creation.md

special-mocking.md

stubbing.md

verification.md

tile.json