Skip to content

Commit 9238c4c

Browse files
author
Anton Keks, Dmitri Ess
committed
add simpler capture() method with lambda
1 parent ce93783 commit 9238c4c

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,10 @@ fun <T> Stubber.whenever(mock: T) = `when`(mock)
5858

5959
inline fun <reified T : Any> argumentCaptor() = ArgumentCaptor.forClass(T::class.java)
6060
inline fun <reified T : Any> capture(captor: ArgumentCaptor<T>): T = captor.capture() ?: createInstance<T>()
61+
inline fun <reified T : Any> capture(noinline consumer: (T) -> Unit): T {
62+
var times = 0
63+
return argThat { if (++times == 1) consumer.invoke(this); true }
64+
}
6165

6266
inline fun <reified T : Any> eq(value: T) = Mockito.eq(value) ?: createInstance<T>()
6367
inline fun <reified T : Any> anyArray(): Array<T> = Mockito.any(Array<T>::class.java) ?: arrayOf()

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

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import java.util.*
88

99
class ArgumentCaptorTest {
1010
@Test
11-
fun captor() {
11+
fun explicitCaptor() {
1212
val date: Date = mock()
1313
val time = argumentCaptor<Long>()
1414

@@ -17,4 +17,14 @@ class ArgumentCaptorTest {
1717
verify(date).time = capture(time)
1818
expect(time.value).toBe(5L)
1919
}
20+
21+
@Test
22+
fun implicitCaptor() {
23+
val date: Date = mock()
24+
date.time = 5L
25+
26+
verify(date).time = capture {
27+
expect(it).toBe(5L)
28+
}
29+
}
2030
}

0 commit comments

Comments
 (0)