explaingit

mockk/mockk

5,744KotlinAudience · developerComplexity · 2/5Setup · easy

TLDR

Testing library for Kotlin that lets you create controlled fake versions of classes and interfaces to isolate the code you are testing, with native support for Kotlin coroutines and Android projects.

Mindmap

mindmap
  root((MockK))
    What It Does
      Mock objects
      Stub responses
      Verify calls
    Key Features
      Coroutine support
      Android support
      BDD add-on
    Use Cases
      Unit testing
      Isolate dependencies
      Async code testing
    Tech Stack
      Kotlin JVM
      Android
      Gradle Maven
Click or tap to explore — scroll the page freely

Code map

Detail Auto

An interactive map of this repo's files and how they connect — its source is parsed live in your browser. Click Visualize to build it.

filefunction / class

Things people build with this

USE CASE 1

Write unit tests that replace a database or network dependency with a fake that returns predictable results, isolating the code under test.

USE CASE 2

Test Kotlin coroutine-based async code by mocking suspending functions, which older Java mocking tools cannot handle.

USE CASE 3

Verify that specific methods were called with expected arguments after running a piece of code.

USE CASE 4

Add BDD-style given/when/then test structure to a Kotlin or Android project using the optional add-on module.

Tech stack

KotlinJVMAndroidGradle

Getting it running

Difficulty · easy Time to first run · 30min

Inline functions cannot be mocked due to Kotlin compilation, known issues with spy objects and suspending functions on JDK 16+.

In plain English

MockK is a testing library for the Kotlin programming language. Its purpose is to let you write automated tests for your code by creating fake versions of the objects your code depends on, so you can control exactly what those fakes return and verify that your code interacted with them correctly. When you write a test, you often want to isolate the piece of code you are testing from everything else it relies on, like a database or a network service. MockK lets you create a stand-in for any class or interface, tell that stand-in how to respond when specific methods are called, run the code you are testing, and then check afterward that the stand-in was called in the way you expected. The syntax is designed to read naturally in Kotlin, using the language's built-in support for function blocks rather than the annotation-heavy style common in Java testing frameworks. The library supports Kotlin's coroutines (its system for handling asynchronous work), which is something older Java-based mocking tools cannot handle well. It also supports Android projects, not just standard JVM applications. For teams that prefer a Behavior-Driven Development style, where tests are structured as given/when/then scenarios, there is an optional add-on module that provides that vocabulary. Some limitations are noted in the README: inline functions cannot be mocked due to how Kotlin compiles them, and there are known issues when using spy objects with suspending functions. There are also compatibility notes for running on newer Java versions (JDK 16 and above). The README is very extensive, covering the full API with code examples, and links to a large number of articles, video tutorials, and community guides in English, Japanese, and Chinese. The library is available through standard Kotlin and Android build tools by adding a single test dependency. The full README is longer than what was shown.

Copy-paste prompts

Prompt 1
Using MockK in Kotlin, show me how to mock a UserRepository interface and make it return a specific user when findById is called.
Prompt 2
How do I use MockK to verify that a method was called exactly twice with a particular argument in my Kotlin unit test?
Prompt 3
Write a MockK test for a Kotlin coroutine function that calls a suspending method on a mocked dependency.
Prompt 4
Using MockK's BDD-style API, write a given/when/then test for a Kotlin service class that sends an email notification.
Open on GitHub → Explain another repo

← mockk on gitmyhub — every repo by this author, as a profile.

Verify against the repo before relying on details.