File tree Expand file tree Collapse file tree 2 files changed +42
-0
lines changed
mockito-kotlin/src/main/kotlin/org/mockito/kotlin
tests/src/test/kotlin/test Expand file tree Collapse file tree 2 files changed +42
-0
lines changed Original file line number Diff line number Diff line change @@ -190,6 +190,13 @@ class KArgumentCaptor(
190
190
val lastValue: T
191
191
get() = captor.lastValue
192
192
193
+ /* *
194
+ * The *only* captured value of the argument,
195
+ * or throws an exception if no value or more than one value was captured.
196
+ */
197
+ val singleValue: T
198
+ get() = captor.singleValue
199
+
193
200
val allValues: List <T >
194
201
get() = captor.allValues
195
202
@@ -223,3 +230,6 @@ val ArgumentCaptor.thirdValue: T
223
230
224
231
val <T > ArgumentCaptor <T >.lastValue: T
225
232
get() = allValues.last()
233
+
234
+ val <T > ArgumentCaptor <T >.singleValue: T
235
+ get() = allValues.single()
Original file line number Diff line number Diff line change @@ -3,6 +3,9 @@ package test
3
3
import com.nhaarman.expect.expect
4
4
import com.nhaarman.expect.expectErrorWithMessage
5
5
import org.junit.Test
6
+ import org.mockito.ArgumentCaptor
7
+ import org.mockito.ArgumentMatchers
8
+ import org.mockito.ArgumentMatchers.anyLong
6
9
import org.mockito.kotlin.*
7
10
import java.util.*
8
11
@@ -126,6 +129,35 @@ class ArgumentCaptorTest : TestBase() {
126
129
expect(captor.lastValue).toBeNull()
127
130
}
128
131
132
+ @Test
133
+ fun argumentCaptor_singleValue () {
134
+ /* Given */
135
+ val date: Date = mock()
136
+
137
+ /* When */
138
+ date.time = 5L
139
+
140
+ /* Then */
141
+ val captor = argumentCaptor<Long >()
142
+ verify(date).time = captor.capture()
143
+ expect(captor.singleValue).toBe(5L )
144
+ }
145
+
146
+ @Test(expected = IllegalArgumentException ::class )
147
+ fun argumentCaptor_singleValue_properlyFails () {
148
+ /* Given */
149
+ val date: Date = mock()
150
+ val captor = argumentCaptor<Long >()
151
+ doNothing().whenever(date).time = captor.capture()
152
+
153
+ /* When */
154
+ date.time = 5L
155
+ date.time = 7L
156
+
157
+ /* Then */
158
+ expect(captor.singleValue).toBe(5 )
159
+ }
160
+
129
161
@Test
130
162
fun argumentCaptor_multipleValues () {
131
163
/* Given */
You can’t perform that action at this time.
0 commit comments