From 9a3b7de2929e1cdf1d56e8595b07ce3ef2be8cb2 Mon Sep 17 00:00:00 2001 From: Niek Haarman Date: Fri, 9 Sep 2016 12:30:15 +0200 Subject: [PATCH 1/3] Introduce argForWhich() as an alias for argThat() --- .../main/kotlin/com/nhaarman/mockito_kotlin/Mockito.kt | 1 + mockito-kotlin/src/test/kotlin/MockitoTest.kt | 9 +++++++++ 2 files changed, 10 insertions(+) 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..d23806c1 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 @@ -43,6 +43,7 @@ inline fun any() = Mockito.any(T::class.java) ?: createInstanc inline fun anyArray(): Array = Mockito.any(Array::class.java) ?: arrayOf() inline fun anyVararg(): T = Mockito.any() ?: createInstance() 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/MockitoTest.kt b/mockito-kotlin/src/test/kotlin/MockitoTest.kt index e5c2eb22..2c500603 100644 --- a/mockito-kotlin/src/test/kotlin/MockitoTest.kt +++ b/mockito-kotlin/src/test/kotlin/MockitoTest.kt @@ -133,6 +133,15 @@ class MockitoTest { }) } } + @Test + fun listArgForWhich() { + mock().apply { + closedList(listOf(Closed(), Closed())) + verify(this).closedList(argForWhich { + size == 2 + }) + } + } @Test fun atLeastXInvocations() { From 6862ea50ffeb79c5920146a0e196c23550753234 Mon Sep 17 00:00:00 2001 From: John Shelley Date: Sat, 17 Sep 2016 22:26:32 -0500 Subject: [PATCH 2/3] update to RC.1 --- mockito-kotlin/build.gradle | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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" From 935184006b5a9951ee0e448da23d74e7c101269a Mon Sep 17 00:00:00 2001 From: Niek Haarman Date: Wed, 21 Sep 2016 19:21:36 +0200 Subject: [PATCH 3/3] Include `anyOrNull()` --- .../com/nhaarman/mockito_kotlin/Mockito.kt | 7 ++++++- mockito-kotlin/src/test/kotlin/Classes.kt | 1 + mockito-kotlin/src/test/kotlin/MockitoTest.kt | 17 +++++++++++++++++ 3 files changed, 24 insertions(+), 1 deletion(-) 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 d23806c1..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,9 +39,14 @@ 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) 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 2c500603..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 { @@ -133,6 +149,7 @@ class MockitoTest { }) } } + @Test fun listArgForWhich() { mock().apply {