Pure Kotlin Unit Testing: Mocking — part 2

Cedric Ferry
5 min readSep 12, 2021

In the previous article we have covered basic Unit Testing with Kotlin test framework. If you missed it, you might want to read it first.

Using pure Kotlin framework and library break your dependency to Java and therefore set you up for Multiplatform. This is obviously useful for Kotlin backend project too.

Pure Kotlin Mocking

Today we are going to look how we can write more elaborate unit tests by using a mocking library.

The most popular pure Kotlin library for mocking is mockK. It scores an impressive 4k+ stars on github.

MockK currently support two essential mocking styles:

  • Mocking
  • Spying

Which mean, no faking or stubbing for now.

However mockK offers mocking “relaxed" that we will detail later.

Brief introduction to Mocking styles

  • Fake objects with working implementations, but the implementation is mostly empty/complying to interface signature without logic.
  • Stubs provide predefined responses to calls made by the test, usually not responding at all to anything outside what’s programmed in for the test.
  • Spies are like stubs that…

--

--

Cedric Ferry
Cedric Ferry

Written by Cedric Ferry

Android Developer @ TikTok, ex-Google

No responses yet