Skip to content

Commit cf06555

Browse files
authored
Merge pull request #72 from nhaarman/any-or-null
Include `anyOrNull()`
2 parents 3d64888 + 9351840 commit cf06555

File tree

3 files changed

+24
-1
lines changed

3 files changed

+24
-1
lines changed

mockito-kotlin/src/main/kotlin/com/nhaarman/mockito_kotlin/Mockito.kt

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,14 @@ import kotlin.reflect.KClass
3939

4040
fun after(millis: Long) = Mockito.after(millis)
4141

42+
/** Matches any object, excluding nulls. */
4243
inline fun <reified T : Any> any() = Mockito.any(T::class.java) ?: createInstance<T>()
43-
inline fun <reified T : Any?> anyArray(): Array<T> = Mockito.any(Array<T>::class.java) ?: arrayOf()
44+
/** Matches anything, including nulls. */
45+
inline fun <reified T : Any> anyOrNull(): T = Mockito.any<T>() ?: createInstance<T>()
46+
/** Matches any vararg object, including nulls. */
4447
inline fun <reified T : Any> anyVararg(): T = Mockito.any<T>() ?: createInstance<T>()
48+
/** Matches any array of type T. */
49+
inline fun <reified T : Any?> anyArray(): Array<T> = Mockito.any(Array<T>::class.java) ?: arrayOf()
4550
inline fun <reified T : Any> argThat(noinline predicate: T.() -> Boolean) = Mockito.argThat<T> { it -> (it as T).predicate() } ?: createInstance(T::class)
4651
inline fun <reified T : Any> argForWhich(noinline predicate: T.() -> Boolean) = argThat(predicate)
4752

mockito-kotlin/src/test/kotlin/Classes.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ interface Methods {
5151
fun string(s: String)
5252
fun closedVararg(vararg c: Closed)
5353
fun throwableClass(t: ThrowableClass)
54+
fun nullableString(s: String?)
5455

5556
fun stringResult(): String
5657
}

mockito-kotlin/src/test/kotlin/MockitoTest.kt

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,22 @@ class MockitoTest {
116116
}
117117
}
118118

119+
@Test
120+
fun anyNull_neverVerifiesAny() {
121+
mock<Methods>().apply {
122+
nullableString(null)
123+
verify(this, never()).nullableString(any())
124+
}
125+
}
126+
127+
@Test
128+
fun anyNull_verifiesAnyOrNull() {
129+
mock<Methods>().apply {
130+
nullableString(null)
131+
verify(this).nullableString(anyOrNull())
132+
}
133+
}
134+
119135
@Test
120136
fun anyThrowableWithSingleThrowableConstructor() {
121137
mock<Methods>().apply {
@@ -133,6 +149,7 @@ class MockitoTest {
133149
})
134150
}
135151
}
152+
136153
@Test
137154
fun listArgForWhich() {
138155
mock<Methods>().apply {

0 commit comments

Comments
 (0)