title | description |
---|---|
MockK Android support |
MockK supports regular unit tests, Android instrumented tests via subclassing(< Android P) and Android instrumented tests via inlining(≥ Android P) |
MockK supports:
- regular unit tests
- Android instrumented tests via subclassing(< Android P)
- Android instrumented tests via inlining(≥ Android P)
To open classes before Android P you can use DexOpener, example
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 and that means you need 'all-open' plugin.
Unfortunately, public CIs alike Travis and Circle are not supporting emulation of Android P because of absense of ARM Anroid P images. Hope this will change soon.
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 | ✓ | ✓ |
All you need to get started is just to add a dependency to MockK
library.
testImplementation "io.mockk:mockk:{version}"
androidTestImplementation "io.mockk:mockk-android:{version}"
Check full documentation here