Mockito for Kotlin
thecodewarrior edited this page Apr 16, 2018
·
3 revisions
This wiki contains brainstorming and plan to provide first class support for Kotlin language. Mockito currently works with Kotlin, but the syntax is not optimal.
- Kotlin has very nice lambda support which creates opportunities to improve Mockito API.
- In Kotlin, all classes are final by default. Mockito cannot mock final class by default (#657).
- Kotlin support can be similar (same?) as Java8 support. Currently, Mockito supports Java8 lambdas via separate GitHub project maintained by Marcin Zajączkowski.
- There is an existing mockito-kotlin library, maintained by Niek Haarman, see the GitHub project.
//mock creation
val mock = mock<MyClass> {
when { getText() } thenReturn "text"
given { getText() } willReturn "text"
}
val mock = mock<MyClass>(withSettings().stubOnly()) {
//...
}
//stubbing / expecting
given(mock.foo(argThat { x < 100 })).willReturn("x")
//verification
verify {
mock.foo(any())
mock.bar()
mock.baz(argThat { x > 100 })
}