File tree Expand file tree Collapse file tree 4 files changed +52
-10
lines changed
main/kotlin/com/nhaarman/mockito_kotlin Expand file tree Collapse file tree 4 files changed +52
-10
lines changed Original file line number Diff line number Diff line change 26
26
package com.nhaarman.mockito_kotlin
27
27
28
28
import org.mockito.ArgumentCaptor
29
+ import kotlin.reflect.KClass
30
+
31
+ inline fun <reified T : Any > argumentCaptor (): KArgumentCaptor <T > = KArgumentCaptor (ArgumentCaptor .forClass(T ::class .java), T ::class )
29
32
30
- inline fun <reified T : Any > argumentCaptor () = ArgumentCaptor .forClass(T ::class .java)
31
33
inline fun <reified T : Any > capture (captor : ArgumentCaptor <T >): T = captor.capture() ? : createInstance<T >()
34
+
35
+ @Deprecated(" Use captor.capture() instead." , ReplaceWith (" captor.capture()" ))
36
+ inline fun <reified T : Any > capture (captor : KArgumentCaptor <T >): T = captor.capture()
37
+
38
+ class KArgumentCaptor <out T : Any >(private val captor : ArgumentCaptor <T >, private val tClass : KClass <T >) {
39
+
40
+ val value: T
41
+ get() = captor.value
42
+
43
+ val allValues: List <T >
44
+ get() = captor.allValues
45
+
46
+ fun capture (): T = captor.capture() ? : createInstance(tClass)
47
+ }
48
+
49
+ /* *
50
+ * This method is deprecated because its behavior differs from the Java behavior.
51
+ * Instead, use [argumentCaptor] in the traditional way, or use one of
52
+ * [argThat], [argForWhich] or [check].
53
+ */
54
+ @Deprecated(" Use argumentCaptor() or argThat() instead." )
32
55
inline fun <reified T : Any > capture (noinline consumer : (T ) -> Unit ): T {
33
56
var times = 0
34
57
return argThat { if (++ times == 1 ) consumer.invoke(this ); true }
Original file line number Diff line number Diff line change @@ -53,6 +53,7 @@ inline fun <reified T : Any?> anyArray(): Array<T> = Mockito.any(Array<T>::class
53
53
54
54
inline fun <reified T : Any > argThat (noinline predicate : T .() -> Boolean ) = Mockito .argThat<T > { it -> (it as T ).predicate() } ? : createInstance(T ::class )
55
55
inline fun <reified T : Any > argForWhich (noinline predicate : T .() -> Boolean ) = argThat(predicate)
56
+ inline fun <reified T : Any > check (noinline predicate : (T ) -> Unit ) = Mockito .argThat<T > { it -> predicate(it); true } ? : createInstance(T ::class )
56
57
57
58
fun atLeast (numInvocations : Int ): VerificationMode = Mockito .atLeast(numInvocations)!!
58
59
fun atLeastOnce (): VerificationMode = Mockito .atLeastOnce()!!
Original file line number Diff line number Diff line change
1
+ import com.nhaarman.expect.expect
1
2
import com.nhaarman.mockito_kotlin.argumentCaptor
2
3
import com.nhaarman.mockito_kotlin.mock
4
+ import com.nhaarman.mockito_kotlin.times
3
5
import com.nhaarman.mockito_kotlin.verify
4
- import com.nhaarman.expect.expect
5
- import com.nhaarman.mockito_kotlin.capture
6
6
import org.junit.Test
7
7
import java.util.*
8
8
9
9
class ArgumentCaptorTest {
10
10
11
11
@Test
12
12
fun explicitCaptor () {
13
+ /* Given */
13
14
val date: Date = mock()
14
- val time = argumentCaptor<Long >()
15
15
16
+ /* When */
16
17
date.time = 5L
17
18
18
- verify(date).time = capture(time)
19
- expect(time.value).toBe(5L )
19
+ /* Then */
20
+ val captor = argumentCaptor<Long >()
21
+ verify(date).time = captor.capture()
22
+ expect(captor.value).toBe(5L )
20
23
}
21
24
22
25
@Test
23
- fun implicitCaptor () {
26
+ fun argumentCaptor_multipleValues () {
27
+ /* Given */
24
28
val date: Date = mock()
29
+
30
+ /* When */
25
31
date.time = 5L
32
+ date.time = 7L
26
33
27
- verify(date).time = capture {
28
- expect(it).toBe(5L )
29
- }
34
+ /* Then */
35
+ val captor = argumentCaptor<Long >()
36
+ verify(date, times(2 )).time = captor.capture()
37
+ expect(captor.allValues).toBe(listOf (5 , 7 ))
30
38
}
31
39
}
Original file line number Diff line number Diff line change @@ -162,6 +162,16 @@ class MockitoTest {
162
162
}
163
163
}
164
164
165
+ @Test
166
+ fun listArgCheck () {
167
+ mock<Methods >().apply {
168
+ closedList(listOf (Closed (), Closed ()))
169
+ verify(this ).closedList(check {
170
+ expect(it.size).toBe(2 )
171
+ })
172
+ }
173
+ }
174
+
165
175
@Test
166
176
fun atLeastXInvocations () {
167
177
mock<Methods >().apply {
You can’t perform that action at this time.
0 commit comments