Understanding MockK Kotlin for Beginners

Richa Sharma
4 min readDec 26, 2021
source : Google

Hello Everyone, in this blog we will go through basic understanding on MockK library(Kotlin).

If you are beginner for writing unit test cases you can go through my blog : Android: Unit Testing for Beginners for basic understanding.

  • Before starting with MockK understanding we should know what is mocking, why we need mocking, how mocking works?

What is Mocking?

  • Mocking is a process used in unit testing when the unit being tested has external dependencies. The purpose of mocking is to isolate and focus on the code being tested and not on the behavior or state of external dependencies.

How Mocking works?

  • A mock is a type of test double that has expectations about its interactions, and whose behavior you can define.
  • It is a dummy implementation for an interface or class. It also allows to define the output of certain method calls.
  • It verify that if you want to check if a certain method of a mock object has been called or not.

Why we need Mocking?

  • Mocking doesn’t give you hangover because the tests are very readable and they produce clean verification errors.
  • It makes unit tests to be independent of all other dependencies.

— — — — — — — — — — — — — — — — — — — — — — — — — — — — — —

MockK

  • Now let’s understand MockK from basics.
sorce: mockk.io
  • MockK is a new open source library focused on making mocking in Kotlin great.
  • It offers support for Kotlin language features and constructs.
  • Its support for Kotlin features comprehensively.
  • MockK can perform efficiently without any extra things as compare to other mocking framework.

Stubbing

  • Stubbing means creating a dummy version, but a stub only mocks the behavior and not the entire object. It is used when our implementation only communicates with a specific behavior of the object.

Argument matcher is an argument placeholder specifying which variety of values are acceptable for an argument in every and verify constructs.

Let’s go through a basic example to understand the basics:

  • For Gradle, we need to add it as a dependency:
  • Now let’s go through class for which we will write unit testcase:

The main point to have stubs or mocks is to isolate testing one component, which is called System Under Test.

class ExampleTest(
val service1: ServiceDependent1,
val service2: ServiceDependent2
)
  • if we want to substract two values than
fun calculateFunc() = service1.value1 - service2.value2
  • Now test it
@Test
fun calculateSubsValues() {
//Creates a mock object with type ServiceDependent1
val sample1 = mockk<ServiceDependent1>()
//Creates a mock object with type ServiceDependent2
val sample2 = mockk<ServiceDependent2>()
// expected behoviour for getting sample1 value
every { sample1.value1 } returns 12
every { sample2.value2 } returns 6
//initialise
val test = ExampleTest(sample1, sample2)

assertEquals(6, test.calculate())
//we verify that result of executing a function with dependencies is correct which is true in this case
}

What is verify :

  • Using verify to check that a function was used.
  • Using verify to verify that a function was called looks a lot like using every for stubbing.
  • verifyAll : all calls occurred, regardless of orders
  • verifySequence: the calls occurred in this exact sequence
  • verifyOrder : the calls occurred in this order.Unlike sequence we can skip few calls.

— — — — — — — — — — — — — — — — — — — — — — — — — — — — — —

  • Using every and returns to define behaviour. -> to stub the behaviour

Mockk supports coroutines out of the box.

  • I will write few more blogs for more understanding on mockK library in our next blogs.
source: Google

Happy Reading :)

--

--