Update dependencies

 - Kotlin: 1.0.1-2 -> 1.0.2
 - Mockito: 2.0.48-beta -> 2.0.52-beta
 - Expect.kt: 0.5.0 -> 0.5.1
1 file changed
tree: 9243f8e7d466f2eb859c33bf9d88c3cfd9940807
  1. .github/
  2. gradle/
  3. mockito-kotlin/
  4. .gitignore
  5. .travis.yml
  6. build.gradle
  7. gradle.properties
  8. gradlew
  9. gradlew.bat
  10. LICENSE
  11. README.md
  12. settings.gradle
README.md

Mockito-Kotlin

Download

A small library that provides helper functions to work with Mockito in Kotlin.

Install

Mockito-Kotlin is available on JCenter. For Gradle users, add the following to your build.gradle:

repositories {
    jcenter()
}
dependencies {
    testCompile "com.nhaarman:mockito-kotlin:x.x.x"
}

Examples

Creating mock instances

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

Expecting any value

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

Argument Matchers

Using higher-order functions, you can write very clear expectations about expected values. For example:

Kotlin:

verify(myClass).setItems(argThat{ size == 2 })

Convenience functions

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.