mocking-with-mockk

Mock Kotlin objects, statics, and constructors in JVM unit tests.

303|10|Updated May 15, 2026
One-click install
npx skills add https://github.com/skydoves/android-testing-skills --skill mocking-with-mockk
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: mocking-with-mockk
Source: https://github.com/skydoves/android-testing-skills/tree/main/jvm-tests/mocking/mocking-with-mockk
Command: npx skills add https://github.com/skydoves/android-testing-skills --skill mocking-with-mockk

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

This Skill helps you reliably mock Kotlin collaborators in JVM unit tests when suspend functions, coroutines, and Kotlin object/singletons are involved, so your tests can verify interactions without brittle workarounds.

Core Features & Use Cases

  • Suspend-safe stubbing and verification: Use coEvery { ... } returns ... and coVerify { ... } to avoid runBlocking-style wrappers and to prevent Continuation-matching errors.
  • Kotlin-native mocking coverage: Mock Kotlin object/singleton behavior (mockkObject), statics (mockkStatic), and constructors (mockkConstructor) with proper cleanup.
  • Strict interaction testing with argument capture: Capture arguments via slot<T>(), enforce call ordering using verifySequence / verifyOrder, and guard against unexpected calls using confirmVerified.

Quick Start

Tell the AI to generate a JVM unit-test setup that uses MockK to stub a suspend repository method with coEvery, verify it with coVerify, capture a saved entity with slot, and include unmockkAll() teardown when any static/object/constructor mocking is used.

Frequently Asked Questions about mocking-with-mockk

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I mock suspend functions and coroutines in Kotlin unit tests?

Mock suspend functions in Kotlin unit tests using `coEvery { ... } returns ...` for stubbing and `coVerify { ... }` for verification. This suspend-safe approach avoids `runBlocking` wrappers and prevents Continuation-matching errors.

What is the best way to mock Kotlin object singletons and companion objects?

Mocking Kotlin object singletons and companion objects requires `mockkObject` for instances and `mockkStatic` for statics. You must perform explicit `unmockkAll()` cleanup afterward to clear global mocking side effects.

How do I capture arguments and enforce call ordering in JVM unit tests?

Capture arguments in JVM unit tests using `slot<T>()` and enforce strict interaction contracts with `verifySequence` or `verifyOrder`. You can also guard against unexpected calls using `confirmVerified`.

Why do I need unmockkAll cleanup when mocking statics or constructors?

You need `unmockkAll()` cleanup when mocking statics or constructors because these operations apply global mocking state. Without explicit teardown, mocked Kotlin objects and constructors will leak into subsequent tests.

Does MockK work with JUnit for strict interaction testing on the JVM?

MockK works with JUnit for strict interaction testing on the JVM by providing argument capture via `slot<T>()` and enforcing call ordering using `verifySequence` to verify interactions without brittle workarounds.