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
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
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
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
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
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
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