Mockk for Kotlin: Complete Guide with Examples

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Introduction to Mockk: What It Is and Why You Need It

\n\n

Mockk is a modern library for mocking (creating object imitations) in the Kotlin programming language. It is specifically designed with Kotlin's features in mind: nullability, extension functions, coroutines, companion objects, and other capabilities that are absent in Java.

\n\n

Why do you need the Mockk library? When writing unit tests, we often encounter situations where the class under test depends on other classes, services, databases, or external APIs. To isolate the tested code and make tests fast, reliable, and independent of external factors, we create "mocks" (mock objects) — stub objects that simulate the behavior of real dependencies.

\n\n

Main features of Mockk:

\n
    \n
  • Creating mocks for classes, interfaces, and objects
  • \n
  • Annotation support (@MockK, @RelaxedMockK, @SpyK)
  • \n
  • Coroutine support (coEvery, coVerify)
  • \n
  • Mocking extension functions
  • \n
  • Mocking static methods and companion objects
  • \n
  • Slot support and argument capturing
  • \n
  • Partial mocking support (spy)
  • \n
\n\n

Installing Mockk

\n\n

Mockk is distributed via Maven Central. To add it to your project, include the dependency in the build.gradle.kts file (for Gradle with Kotlin DSL):

\n\n
// build.gradle.kts\ndependencies {\n    testImplementation("io.mockk:mockk:1.13.10")\n}
\n\n

For Gradle with Groovy DSL:

\n\n
// build.gradle\ndependencies {\n    testImplementation "io.mockk:mockk:1.13.10"\n}
\n\n

For Maven:

\n\n
<dependency>\n    <groupId>io.mockk</groupId>\n    <artifactId>mockk</artifactId>\n    <version>1.13.10</version>\n    <scope>test</scope>\n</dependency>
\n\n

If you use coroutines, no additional setup is needed — coroutine support is built into Mockk.

\n\n

Quick Start with Code Example

\n\n

Let's look at a simple example. Suppose we have a service UserService that depends on a repository UserRepository:

\n\n
// Classes for testing\nclass UserRepository {\n    fun findById(id: Long): String {\n        // Real implementation accesses the database\n        return "Real User"\n    }\n}\n\nclass UserService(private val repository: UserRepository) {\n    fun getUserName(id: Long): String {\n        val user = repository.findById(id)\n        return if (user.isNotEmpty()) user else "Unknown"\n    }\n}
\n\n

Now let's write a test using Mockk:

\n\n
import io.mockk.every\nimport io.mockk.mockk\nimport io.mockk.verify\nimport org.junit.jupiter.api.Test\n\nclass UserServiceTest {\n\n    @Test\n    fun `getUserName should return user name from repository`() {\n        // 1. Create a mock for the repository\n        val mockRepository = mockk<UserRepository>()\n        \n        // 2. Define the mock's behavior\n        every { mockRepository.findById(1L) } returns "John Doe"\n        \n        // 3. Create the object under test with the mock\n        val service = UserService(mockRepository)\n        \n        // 4. Call the method under test\n        val result = service.getUserName(1L)\n        \n        // 5. Verify the result\n        assert(result == "John Doe")\n        \n        // 6. Verify that the method was called exactly once\n        verify(exactly = 1) { mockRepository.findById(1L) }\n    }\n}
\n\n

Main Methods and Functions of Mockk

\n\n

Let's look at 10 key library methods with detailed descriptions and examples.

\n\n

1. mockk() — Creating a Mock Object

\n

What it does: Creates a mock object for the specified class or interface. By default, all methods of the mock return default values (null, 0, false) and throw an exception if

Recommendations