Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Kotlin MockK Basics

Kotlin MockK Basics

In this talk you will learn the basic fundamentals, you will need to get started with MockK and be productive.

Avatar for Rowland Oti

Rowland Oti

May 22, 2020
Tweet

More Decks by Rowland Oti

Other Decks in Programming

Transcript

  1. © 2019 KOKO Networks Limited – Proprietary & Confidential Kotlin

    Mockk 1. Relaxed and strict mock 2. Verification and confirmation 3. Verification with timeout 4. Mock private fields 5. Mock Kotlin objects 6. Capturing parameters
  2. © 2019 KOKO Networks Limited – Proprietary & Confidential Mocking

    is not Rocket Science? Brief: MockK library, offers support for Kotlin language features and constructs. The overall benefits that MockK gives us are worth it. Is it Better than Mockito: MockK is definitely a better alternative to other mocking frameworks if you use Kotlin. Explore Kotlin features like DSL syntax. Allows you to write code in DSL syntax, making test much more readable. 2
  3. © 2019 KOKO Networks Limited – Proprietary & Confidential Relaxed

    Mock vs Strict Mock Relaxed Mock: A relaxed mock is the mock that returns some simple value for all functions. This allows to skip specifying behavior for each case, while still allowing to stub things you need. For reference types, chained mocks are returned. Strict Mock: Forces you to have answer for every method call on the mocked object Note: Mockito generally is relaxed mock by default. For Mockk, we have to override that, as it is strict by default. We can override this behaviour globally per class
  4. © 2019 KOKO Networks Limited – Proprietary & Confidential Verification

    and Confirmation Good to confirm verification: To double check that all calls were verified by verify, increases the test fidelity. Always a good idea to do for cases where it makes sense. 4
  5. © 2019 KOKO Networks Limited – Proprietary & Confidential Verification

    with timeout Methods that do background jobs: As the code runs sequentially, we may have scenarios where the test cases may behave in unpredictable manner (pass or fail), since executions times may differ Mocck provides a way to wait for the execution, but this is good when we have a rough idea about how long it could take to execute. 5 Background job
  6. © 2019 KOKO Networks Limited – Proprietary & Confidential Mock

    private fields? Private by default: In Kotlin, properties are always accessed through getter and setter, thus there's no need in making a property private with public accessors like in Java -- its backing field (if present) is already private. So, visibility modifiers on property accessors are only used to make setter visibility less permissive, and in most cases is redundant Therefore issue of mocking private fields or property in Kotlin does not arise. 6
  7. © 2019 KOKO Networks Limited – Proprietary & Confidential Mock

    Kotlin objects Can we override behavior of static objects? Most of the mocking libraries have a problem with mocking Kotlin singleton instances, Mockito included. 7
  8. © 2019 KOKO Networks Limited – Proprietary & Confidential Capturing

    Parameters Description: If we need to capture the parameters passed to a method, then we can use CapturingSlot or MutableList. It is useful when we want to have some custom logic in an answer block or we just need to verify the value of the arguments passed 8
  9. © 2019 KOKO Networks Limited – Proprietary & Confidential Next

    steps 1. Check out the Mockk website and blogs on medium 2. Play around with the code 3. Improve unit tests for existing code base 9
  10. © 2019 KOKO Networks Limited – Proprietary & Confidential For

    more information, contact: Otieno Odhiambo o.odhiambo2@kokonetworks.com