-
Notifications
You must be signed in to change notification settings - Fork 207
Comparison to MockK
In the Kotlin world the most popular mocking frameworks are MockK (5.7k stars) and Mockito-Kotlin (3.2k stars).
MockK was built from the ground-up to be Kotlin first. In contrast, Mockito-Kotlin began as a thin wrapper around Mockito, a Java library. Historically, MockK expressed a more fluent Kotlin API and had better support for Kotlin features like coroutines, objects, and value classes.
Modern releases of Mockito-Kotlin have refined the API and added missing features. Both frameworks are good choices in Kotlin projects.
Both Mockito-Kotlin and MockK support the the core features you expect from a mocking framework.
| Feature | Mockito-Kotlin | MockK |
|---|---|---|
| Mocking | mock<Foo>() |
mockk<Foo>() |
| Stubbing |
whenever(mock.foo()).thenReturn(x)or mock<Foo> { on { foo() } doReturn x }
|
every { mock.foo() } return x |
| Verification | verify(mock).foo() |
verify { mock.foo() } |
Modern Mockito-Kotlin has support for advanced mocking including common Kotlin language types:
| Feature | Mockito-Kotlin | MockK |
|---|---|---|
| Object/Companion object | mockObject(MyObject) |
mockkObject(MyObject) |
| Extension funs | mockExtensionFun(MyObject::extensionFun) |
mockkStatic(MyObject::extensionFun) |
| Constructor | mockConstruction<MyClass> |
mockkConstructor(MyClass::class) |
This repo includes some benchmarks comparing the runtime performance of Mockito-Kotlin to MockK. See: benchmarks/README.md for details.
For the "simple" and "heavy" benchmarks, Mockito-Kotlin appears 10-40% faster on a cold iteration (this includes classloading + JVM warmup). Mockito-Kotlin appears 2-3x faster than MockK on warm iterations.
For the "object" benchmarks which compares mockObject() and MockK's mockkObject(), we find Mockito-Kotlin is significantly faster (0.2ms vs. 35.9ms per iteration), likely due to the extra instrumentation MockK does after unmockkObject().
Note these benchmarks are not authoritative, and we invite discussion and contributions to improve them.
Both MockK and Mockito-Kotlin support Android projects. (See Android-Support.)
They use the same dexmaker agent under the hood to support creating mocks on the Android Runtime (ART).