Skip to content

What is MockK

Devrath edited this page Mar 2, 2024 · 4 revisions

Mocking Means

  • When we are testing a class, There are dependencies present that are needed for the functioning of the class.
  • So when we are testing that class, we can make it such that the dependencies return a stubbed result as we expect for that test case.
  • So when we are writing the test case, actually we will be testing the SUT(system under test) rather than the dependencies itself.

About MockK

  • MockK is a mocking framework used in kotlin for writing unit tests.
  • It is used for creating mock objects of real objects.
  • We can mock the objects and modify the properties.
  • In the mock object, We can Stub the methods and verify the interactions.
  • It supports all the features of kotlin.

Problems with Mockito that enables us to prefer MockK

  • Mockito cannot mock or spy final classes
    • Possible Solution:
      • We could make the class open or use interfaces but it requires modifying the code.
      • We could use mockito-kotlin wrapper but it requires additional boilerplate code.
      • Mockito doesn't support private static functions instead we might need to use power-mock for this.
    • Alternative:mockK is a better option for Mockito.
  • MockK is built for kotlin.