diff --git a/mockito-kotlin/build.gradle b/mockito-kotlin/build.gradle index d35e7106..f8a150f9 100644 --- a/mockito-kotlin/build.gradle +++ b/mockito-kotlin/build.gradle @@ -20,7 +20,7 @@ repositories { dependencies { compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version" compile "org.jetbrains.kotlin:kotlin-reflect:$kotlin_version" - compile "org.mockito:mockito-core:2.1.0-beta.125" + compile "org.mockito:mockito-core:2.1.0-RC.1" /* Tests */ testCompile "junit:junit:4.12" diff --git a/mockito-kotlin/src/main/kotlin/com/nhaarman/mockito_kotlin/Mockito.kt b/mockito-kotlin/src/main/kotlin/com/nhaarman/mockito_kotlin/Mockito.kt index 3be50d90..7b520fe6 100644 --- a/mockito-kotlin/src/main/kotlin/com/nhaarman/mockito_kotlin/Mockito.kt +++ b/mockito-kotlin/src/main/kotlin/com/nhaarman/mockito_kotlin/Mockito.kt @@ -39,10 +39,16 @@ import kotlin.reflect.KClass fun after(millis: Long) = Mockito.after(millis) +/** Matches any object, excluding nulls. */ inline fun any() = Mockito.any(T::class.java) ?: createInstance() -inline fun anyArray(): Array = Mockito.any(Array::class.java) ?: arrayOf() +/** Matches anything, including nulls. */ +inline fun anyOrNull(): T = Mockito.any() ?: createInstance() +/** Matches any vararg object, including nulls. */ inline fun anyVararg(): T = Mockito.any() ?: createInstance() +/** Matches any array of type T. */ +inline fun anyArray(): Array = Mockito.any(Array::class.java) ?: arrayOf() inline fun argThat(noinline predicate: T.() -> Boolean) = Mockito.argThat { it -> (it as T).predicate() } ?: createInstance(T::class) +inline fun argForWhich(noinline predicate: T.() -> Boolean) = argThat(predicate) fun atLeast(numInvocations: Int): VerificationMode = Mockito.atLeast(numInvocations)!! fun atLeastOnce(): VerificationMode = Mockito.atLeastOnce()!! diff --git a/mockito-kotlin/src/test/kotlin/Classes.kt b/mockito-kotlin/src/test/kotlin/Classes.kt index 2f066f36..9107ebc3 100644 --- a/mockito-kotlin/src/test/kotlin/Classes.kt +++ b/mockito-kotlin/src/test/kotlin/Classes.kt @@ -51,6 +51,7 @@ interface Methods { fun string(s: String) fun closedVararg(vararg c: Closed) fun throwableClass(t: ThrowableClass) + fun nullableString(s: String?) fun stringResult(): String } diff --git a/mockito-kotlin/src/test/kotlin/MockitoTest.kt b/mockito-kotlin/src/test/kotlin/MockitoTest.kt index e5c2eb22..9d3a5fa4 100644 --- a/mockito-kotlin/src/test/kotlin/MockitoTest.kt +++ b/mockito-kotlin/src/test/kotlin/MockitoTest.kt @@ -116,6 +116,22 @@ class MockitoTest { } } + @Test + fun anyNull_neverVerifiesAny() { + mock().apply { + nullableString(null) + verify(this, never()).nullableString(any()) + } + } + + @Test + fun anyNull_verifiesAnyOrNull() { + mock().apply { + nullableString(null) + verify(this).nullableString(anyOrNull()) + } + } + @Test fun anyThrowableWithSingleThrowableConstructor() { mock().apply { @@ -134,6 +150,16 @@ class MockitoTest { } } + @Test + fun listArgForWhich() { + mock().apply { + closedList(listOf(Closed(), Closed())) + verify(this).closedList(argForWhich { + size == 2 + }) + } + } + @Test fun atLeastXInvocations() { mock().apply {