Skip to content

Commit e43ccea

Browse files
authored
Merge pull request #100 from nhaarman/nullable-argumentcaptor
Add `nullableArgumentCaptor` to be able to work with lists of nullables
2 parents 7b6c5b7 + 086d482 commit e43ccea

File tree

2 files changed

+50
-8
lines changed

2 files changed

+50
-8
lines changed

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,29 +29,31 @@ import org.mockito.ArgumentCaptor
2929
import kotlin.reflect.KClass
3030

3131
inline fun <reified T : Any> argumentCaptor(): KArgumentCaptor<T> = KArgumentCaptor(ArgumentCaptor.forClass(T::class.java), T::class)
32+
inline fun <reified T : Any> nullableArgumentCaptor(): KArgumentCaptor<T?> = KArgumentCaptor(ArgumentCaptor.forClass(T::class.java), T::class)
3233

3334
inline fun <reified T : Any> capture(captor: ArgumentCaptor<T>): T = captor.capture() ?: createInstance<T>()
3435

3536
@Deprecated("Use captor.capture() instead.", ReplaceWith("captor.capture()"), DeprecationLevel.ERROR)
3637
inline fun <reified T : Any> capture(captor: KArgumentCaptor<T>): T = captor.capture()
3738

38-
class KArgumentCaptor<out T : Any>(private val captor: ArgumentCaptor<T>, private val tClass: KClass<T>) {
39+
class KArgumentCaptor<out T : Any?>(private val captor: ArgumentCaptor<T>, private val tClass: KClass<*>) {
3940

4041
val value: T
4142
get() = captor.value
4243

4344
val allValues: List<T>
4445
get() = captor.allValues
4546

46-
fun capture(): T = captor.capture() ?: createInstance(tClass)
47+
@Suppress("UNCHECKED_CAST")
48+
fun capture(): T = captor.capture() ?: createInstance(tClass) as T
4749
}
4850

4951
/**
5052
* This method is deprecated because its behavior differs from the Java behavior.
5153
* Instead, use [argumentCaptor] in the traditional way, or use one of
5254
* [argThat], [argForWhich] or [check].
5355
*/
54-
@Deprecated("Use argumentCaptor() or argThat() instead.", ReplaceWith("check(consumer)"), DeprecationLevel.ERROR)
56+
@Deprecated("Use argumentCaptor(), argThat() or check() instead.", ReplaceWith("check(consumer)"), DeprecationLevel.ERROR)
5557
inline fun <reified T : Any> capture(noinline consumer: (T) -> Unit): T {
5658
var times = 0
5759
return argThat { if (++times == 1) consumer.invoke(this); true }

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

Lines changed: 45 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,12 @@
11
import com.nhaarman.expect.expect
2-
import com.nhaarman.mockito_kotlin.argumentCaptor
3-
import com.nhaarman.mockito_kotlin.mock
4-
import com.nhaarman.mockito_kotlin.times
5-
import com.nhaarman.mockito_kotlin.verify
2+
import com.nhaarman.mockito_kotlin.*
63
import org.junit.Test
74
import java.util.*
85

96
class ArgumentCaptorTest {
107

118
@Test
12-
fun explicitCaptor() {
9+
fun argumentCaptor_withSingleValue() {
1310
/* Given */
1411
val date: Date = mock()
1512

@@ -22,6 +19,34 @@ class ArgumentCaptorTest {
2219
expect(captor.value).toBe(5L)
2320
}
2421

22+
@Test
23+
fun argumentCaptor_withNullValue_usingNonNullable() {
24+
/* Given */
25+
val m: Methods = mock()
26+
27+
/* When */
28+
m.nullableString(null)
29+
30+
/* Then */
31+
val captor = argumentCaptor<String>()
32+
verify(m).nullableString(captor.capture())
33+
expect(captor.value).toBeNull()
34+
}
35+
36+
@Test
37+
fun argumentCaptor_withNullValue_usingNullable() {
38+
/* Given */
39+
val m: Methods = mock()
40+
41+
/* When */
42+
m.nullableString(null)
43+
44+
/* Then */
45+
val captor = nullableArgumentCaptor<String>()
46+
verify(m).nullableString(captor.capture())
47+
expect(captor.value).toBeNull()
48+
}
49+
2550
@Test
2651
fun argumentCaptor_multipleValues() {
2752
/* Given */
@@ -36,4 +61,19 @@ class ArgumentCaptorTest {
3661
verify(date, times(2)).time = captor.capture()
3762
expect(captor.allValues).toBe(listOf(5, 7))
3863
}
64+
65+
@Test
66+
fun argumentCaptor_multipleValuesIncludingNull() {
67+
/* Given */
68+
val m: Methods = mock()
69+
70+
/* When */
71+
m.nullableString("test")
72+
m.nullableString(null)
73+
74+
/* Then */
75+
val captor = nullableArgumentCaptor<String>()
76+
verify(m, times(2)).nullableString(captor.capture())
77+
expect(captor.allValues).toBe(listOf("test", null))
78+
}
3979
}

0 commit comments

Comments
 (0)