commit | 2f9603ae72f6e25dcd9c79ff67d281b6a10ca3e2 | [log] [tgz] |
---|---|---|
author | Niek Haarman <haarman.niek@gmail.com> | Mon Jan 18 21:48:20 2016 +0100 |
committer | Niek Haarman <haarman.niek@gmail.com> | Thu Jan 21 23:45:00 2016 +0100 |
tree | 1e5b55cd77d9b3a38bd099c1d93a350e174019a8 |
Initial commit
A small library that provides helper functions to work with Mockito in Kotlin.
Download a .jar file from the releases page.
Due to Kotlin‘s reified type parameters, if the type can be inferred, you don’t have to specify it explicitly:
Java:
MyClass c = mock(Myclass.class); c.doSomething(mock(MyOtherClass.class));
Kotlin:
val c : MyClass = mock() c.doSomething(mock())
If the type can't be inferred, you can pass it like so:
val d = mock<MyClass>()
Mockito‘s any(Class<T>)
often returns null
for non-primitive classes. In Kotlin, this can be a problem due to its null-safety feature. This library creates non-null instances when necessary. Again, if the type can be inferred, you don’t have to specify it explicitely:
Java:
verify(myClass).doSomething(any(String.class));
Kotlin:
verify(myClass).doSomething(any()); // Non-nullable parameter type is inferred
For generic arrays, use the anyArray()
method:
verify(myClass).setItems(anyArray())
Using higher-order functions, you can write very clear expectations about expected values. For example:
Kotlin:
verify(myClass).setItems(argThat{ size == 2 })
Most of Mockito‘s static functions are available as top-level functions. That means, IDE’s like IntelliJ can easily import and autocomplete them, saving you the hassle of manually importing them.