Need advice about which tool to choose?Ask the StackShare community!


+ 1

+ 1
Add tool

Mockito vs mockk: What are the differences?


In this article, we will be discussing the key differences between Mockito and MockK, two popular mocking frameworks used in Java and Kotlin respectively.

  1. Compatibility and Language Support: Mockito is primarily used for mocking in Java projects, whereas MockK is specifically designed for mocking in Kotlin projects. Mockito supports both Java and Kotlin, but it may not have as native support for certain Kotlin-specific features compared to MockK.

  2. Syntax and API: Mockito uses a more traditional syntax and API with static methods and method chaining. On the other hand, MockK provides a more concise and expressive syntax using Kotlin's DSL (Domain-Specific Language) capabilities, which makes it easier to write and read mocking code.

  3. Nullability Handling: Mockito does not differentiate between nullable and non-nullable types, which means it does not enforce null safety in its API. MockK, being a Kotlin-specific framework, is designed with null safety in mind and provides better support for handling nullable types, allowing developers to avoid potential null pointer exceptions.

  4. Coroutines Support: MockK has built-in support for mocking coroutines, which is a key advantage for Kotlin developers. It provides functions and methods specifically designed to handle coroutine-related scenarios, such as suspending functions and delay operations. Mockito, being a Java-centric framework, does not have out-of-the-box support for mocking coroutines.

  5. Spying vs Mocking: In Mockito, both mocking and spying (partial mocking) are supported. Spying allows you to partially mock an object while still calling the real methods on it. MockK, on the other hand, only supports mocking and does not have built-in support for spying. If you need to perform partial mocking in MockK, you would generally need to resort to other Kotlin-specific techniques.

  6. Verification: Mockito provides various assertion methods for verifying method invocations, such as verify, verifyZeroInteractions, and verifyNoMoreInteractions. MockK also provides similar verification capabilities but with a slightly different syntax using the verify function. MockK's syntax is more concise and follows the Kotlin conventions.

In summary, Mockito is a popular mocking framework for Java that can also be used in Kotlin, whereas MockK is a mocking framework specifically designed for Kotlin. MockK provides a more Kotlin-native syntax, better null safety handling, and built-in support for mocking coroutines, while Mockito offers greater flexibility with spying and a syntax more familiar to Java developers.

Get Advice from developers at your company using StackShare Enterprise. Sign up for StackShare Enterprise.
Learn More

What is Mockito?

It is a mocking framework that tastes really good. It lets you write beautiful tests with a clean & simple API. It doesn’t give you hangover because the tests are very readable and they produce clean verification errors.

What is mockk?

It is an open-source library focused on making mocking in Kotlin great. It is a library with the possibility of mocking default arguments, final classes, varargs, coroutines and extension methods.

Need advice about which tool to choose?Ask the StackShare community!

What companies use Mockito?
What companies use mockk?
See which teams inside your own company are using Mockito or mockk.
Sign up for StackShare EnterpriseLearn More

Sign up to get full access to all the companiesMake informed product decisions

What tools integrate with Mockito?
What tools integrate with mockk?
What are some alternatives to Mockito and mockk?
JUnit is a simple framework to write repeatable tests. It is an instance of the xUnit architecture for unit testing frameworks.
Appium is an open source test automation framework for use with native, hybrid, and mobile web apps. It drives iOS and Android apps using the WebDriver protocol. Appium is sponsored by Sauce Labs and a thriving community of open source developers.
It is a framework that brings fast and reliable unit tests to Android. Tests run inside the JVM on your workstation in seconds. Test drive your Android application with robolectric
JavaScript is most known as the scripting language for Web pages, but used in many non-browser environments as well such as node.js or Apache CouchDB. It is a prototype-based, multi-paradigm scripting language that is dynamic,and supports object-oriented, imperative, and functional programming styles.
Git is a free and open source distributed version control system designed to handle everything from small to very large projects with speed and efficiency.
See all alternatives