mockk kotlin

Support

MockK supports:

DexOpener

To open classes before Android P you can use DexOpener, example

Implementation

Implementation is based on dexmaker project. With Android P, instrumentation tests may use full power of inline instrumentation, so object mocks, static mocks and mocking of final classes are supported. Before Android P, only subclassing can be employed, so you will need the β€˜all-open’ plugin.

Unfortunately, public CIs such as Travis and Circle are not supporting Android P emulation due to the absence of ARM Android P images. Hopefully, this will change soon.

Supported features

Feature Unit tests Instrumentation test
< Android P β‰₯ Android P
annotations βœ“ βœ“ βœ“
mocking final classes βœ“ can use DexOpener βœ“
pure Kotlin mocking DSL βœ“ βœ“ βœ“
matchers partial specification βœ“ βœ“ βœ“
chained calls βœ“ βœ“ βœ“
matcher expressions βœ“ βœ“ βœ“
mocking coroutines βœ“ βœ“ βœ“
capturing lambdas βœ“ βœ“ βœ“
object mocks βœ“ βœ“
private function mocking βœ“ βœ“
property backing field access βœ“ βœ“ βœ“
extension function mocking (static mocks) βœ“ βœ“
constructor mocking βœ“ βœ“

Installation

All you need to get started is by adding a dependency to MockK library.

Unit tests

testImplementation "io.mockk:mockk:{version}"

Android instrumented tests

androidTestImplementation "io.mockk:mockk-android:{version}"

current version

Documentation

Check full documentation here