Testing

There are several testing frameworks that provide Kotlin-based DSLs for writing tests. One such example is MockitoKotlin, which is a small library that was built to provide Kotlin-friendly convenience functions for working with Mockito when writing JUnit tests.

With MockitoKotlin, we can write very readable, Kotlin-idomatic mocking code, as shown in the following code snippet:

val mockGreetingProvider = mock<GreetingProvider> {
on { greeting } doReturn "Hello"
on { friendlyGreeting } doReturn "Hi! How are you?"
}

In this example, we can call a mock() function and pass a lambda that can then be used to mock out the behavior of each property or method on the interface. The on and doReturn functions help us write mocking code that is very human readable and fluent to write.