File tree Expand file tree Collapse file tree 2 files changed +47
-0
lines changed
mockito-kotlin/src/main/kotlin/com/nhaarman/mockitokotlin2
tests/src/test/kotlin/test Expand file tree Collapse file tree 2 files changed +47
-0
lines changed Original file line number Diff line number Diff line change @@ -36,13 +36,27 @@ inline fun <reified T : Any> argumentCaptor(): KArgumentCaptor<T> {
36
36
return KArgumentCaptor (ArgumentCaptor .forClass(T ::class .java), T ::class )
37
37
}
38
38
39
+ /* *
40
+ * Creates a [KArgumentCaptor] for given type, taking in a lambda to allow fast verification.
41
+ */
42
+ inline fun <reified T : Any > argumentCaptor (f : KArgumentCaptor <T >.() -> Unit ): KArgumentCaptor <T > {
43
+ return argumentCaptor<T >().apply (f)
44
+ }
45
+
39
46
/* *
40
47
* Creates a [KArgumentCaptor] for given nullable type.
41
48
*/
42
49
inline fun <reified T : Any > nullableArgumentCaptor (): KArgumentCaptor <T ?> {
43
50
return KArgumentCaptor (ArgumentCaptor .forClass(T ::class .java), T ::class )
44
51
}
45
52
53
+ /* *
54
+ * Creates a [KArgumentCaptor] for given nullable type, taking in a lambda to allow fast verification.
55
+ */
56
+ inline fun <reified T : Any > nullableArgumentCaptor (f : KArgumentCaptor <T ?>.() -> Unit ): KArgumentCaptor <T ?> {
57
+ return nullableArgumentCaptor<T >().apply (f)
58
+ }
59
+
46
60
/* *
47
61
* Alias for [ArgumentCaptor.capture].
48
62
*/
Original file line number Diff line number Diff line change 1
1
package test
2
2
3
3
import com.nhaarman.expect.expect
4
+ import com.nhaarman.expect.expectErrorWithMessage
4
5
import com.nhaarman.mockitokotlin2.*
5
6
import org.junit.Test
6
7
import java.util.*
@@ -117,4 +118,36 @@ class ArgumentCaptorTest : TestBase() {
117
118
expect(secondValue).toBe(2 )
118
119
}
119
120
}
121
+
122
+ @Test
123
+ fun argumentCaptor_withSingleValue_lambda () {
124
+ /* Given */
125
+ val date: Date = mock()
126
+
127
+ /* When */
128
+ date.time = 5L
129
+
130
+ /* Then */
131
+ argumentCaptor<Long > {
132
+ verify(date).time = capture()
133
+ expect(lastValue).toBe(5L )
134
+ }
135
+ }
136
+
137
+ @Test
138
+ fun argumentCaptor_withSingleValue_lambda_properlyFails () {
139
+ /* Given */
140
+ val date: Date = mock()
141
+
142
+ /* When */
143
+ date.time = 5L
144
+
145
+ /* Then */
146
+ expectErrorWithMessage(" Expected: 3 but was: 5" ) on {
147
+ argumentCaptor<Long > {
148
+ verify(date).time = capture()
149
+ expect(lastValue).toBe(3L )
150
+ }
151
+ }
152
+ }
120
153
}
You can’t perform that action at this time.
0 commit comments