Slide 1

Slide 1 text

© 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

Slide 2

Slide 2 text

© 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

Slide 3

Slide 3 text

© 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

Slide 4

Slide 4 text

© 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

Slide 5

Slide 5 text

© 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

Slide 6

Slide 6 text

© 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

Slide 7

Slide 7 text

© 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

Slide 8

Slide 8 text

© 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

Slide 9

Slide 9 text

© 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

Slide 10

Slide 10 text

© 2019 KOKO Networks Limited – Proprietary & Confidential For more information, contact: Otieno Odhiambo [email protected]