Write unit tests that replace a database or network dependency with a fake that returns predictable results, isolating the code under test.
Test Kotlin coroutine-based async code by mocking suspending functions, which older Java mocking tools cannot handle.
Verify that specific methods were called with expected arguments after running a piece of code.
Add BDD-style given/when/then test structure to a Kotlin or Android project using the optional add-on module.
Inline functions cannot be mocked due to Kotlin compilation, known issues with spy objects and suspending functions on JDK 16+.
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.
← mockk on gitmyhub — every repo by this author, as a profile.
Verify against the repo before relying on details.