Skip to main content

Setup

  1. Apply Gradle plugin:
plugins {
kotlin("multiplatform") version "2.0.21" // ...or any other Kotlin plugin
id("dev.mokkery") version "2.5.1"
}
  1. Ensure that your Mokkery version is compatible with your Kotlin version. Refer to the compatibility section.

  2. Check the source sets section to make sure that Mokkery is properly applied in your project.

  3. Check the targets section to make sure that all targets from you multiplatform project are supported.

  4. At this point everything should be fine. In case of Gradle being unable to find dependencies, check missing repositories.

// for runtime dependencies
repositories {
mavenCentral()
}
// for plugin dependencies
pluginManagement {
repositories {
gradlePluginPortal() // required only before Mokkery 2.3.0
mavenCentral()
}
}

Convention plugins

tip

Since Mokkery 2.3.0, it's no longer required to add this repository, because Mokkery Gradle plugin is also published to Maven Central.

For those using convention plugins, make sure that the dependencies configuration includes the path to the required repository, which can be added using gradlePluginPortal().

dependencyResolutionManagement {
repositories {
mavenCentral()
// required to download Mokkery Gradle plugin before version 2.3.0
gradlePluginPortal()
}
}

Once the required repository is connected, you will be able to successfully download the Mokkery plugin dependency.

dependencies {
implementation("dev.mokkery:mokkery-gradle:$mokkeryVersion")
}

Source sets

By default, Mokkery is applied to all Kotlin source sets in the project that either contain the word 'Test' in their names or are named exactly 'test'. As a result, Mokkery is correctly applied to the default test source sets in Android, JVM, or Multiplatform projects. However, this default behavior might need adjustments if any of your main custom source sets contain the word 'Test'.

warning

Before version 2.1.0, Mokkery was applied by default only to source sets whose names contained the phrase 'Test'. In result it was not applied in JVM projects.

Apply for main

If you need to use Mokkery in the main source set (e.g., when mocks need to be extracted to a separate subproject), use the ApplicationRule.All:

mokkery {
rule.set(ApplicationRule.All)
}

This applies Mokkery to all source sets within the current subproject.

Customization

If neither ApplicationRule.AllTests nor ApplicationRule.All meets your requirements, you can manually specify the source sets. The following options are available:

  • ApplicationRule.Listed("fooMain", "barMain") - applies Mokkery only to the explicitly listed source sets.
  • ApplicationRule.MatchesName(Regex(".+Main")) - applies Mokkery to all source sets whose names match the given regular expression.
  • Provide a custom rule to suit your needs.

When selecting source sets manually, ensure that you include all source sets within a subtree. The Mokkery plugin does not automatically detect dependencies between source sets, as these dependencies may not always be present.

Targets

✅ Supported:

You can refer to this file for more details.

Compatibility

warning

Mokkery 2.* versions aren't tied to specific Kotlin 2.* versions, assuming the compiler API will stay stable. However, breaking changes can cause compile-time errors that require updating Mokkery. Track compiler API stability here.

Mokkery versionSupported Kotlin versions
2.3.0 - 2.5.12.0.0 - 2.0.21
2.0.0 – 2.2.02.0.0 - 2.0.10