tamas-bazsonyi.jpg

Mocking without reflection for Kotlin/Multiplatform

Salomon Brys

Mocking typically involves reflection: an efficient mocker can create implementations of interfaces or even concrete classes on the go by reflecting on their methods and properties.
These required capabilities do not exist in Kotlin/Multiplatform, so we will explore an alternative method to runtime reflection: a compile-time symbol processor.
Using Mocking as an excuse, we will explore how we can use KSP (Kotlin Symbol Processor) to circumvent the absence of reflection and generate efficient mocks at compile time. We will also see the limitations that multiplatform brings to both the KSP Mocker generator and its associated runtime. We will explain the choices that we made when developing the MocKMP multiplatform mocking library, and why they were needed. We will then demonstrate the clean DSL API we offer to describe and verify your mocks.
Finally, we will demonstrate how to architecture all these moving parts (KSP plugin, mocker generator, mocker runtime) using a custom Gradle Plugin to make using MocKMP as easy as it gets.