Skip to content

Commit 57d0d8b

Browse files
authored
Merge pull request #118 from nhaarman/improve-argumentcaptor
Add value shortcuts to ArgumentCaptor
2 parents 37f1717 + 81a0996 commit 57d0d8b

File tree

5 files changed

+93
-11
lines changed

5 files changed

+93
-11
lines changed

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

Lines changed: 45 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,21 +33,59 @@ inline fun <reified T : Any> nullableArgumentCaptor(): KArgumentCaptor<T?> = KAr
3333

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

36-
@Deprecated("Use captor.capture() instead.", ReplaceWith("captor.capture()"), DeprecationLevel.ERROR)
37-
inline fun <reified T : Any> capture(captor: KArgumentCaptor<T>): T = captor.capture()
38-
3936
class KArgumentCaptor<out T : Any?>(private val captor: ArgumentCaptor<T>, private val tClass: KClass<*>) {
4037

38+
@Deprecated("Use lastValue", ReplaceWith("lastValue"))
4139
val value: T
4240
get() = captor.value
4341

42+
/**
43+
* The first captured value of the argument.
44+
* @throws IndexOutOfBoundsException if the value is not available.
45+
*/
46+
val firstValue: T
47+
get() = captor.firstValue
48+
49+
/**
50+
* The second captured value of the argument.
51+
* @throws IndexOutOfBoundsException if the value is not available.
52+
*/
53+
val secondValue: T
54+
get() = captor.secondValue
55+
56+
/**
57+
* The third captured value of the argument.
58+
* @throws IndexOutOfBoundsException if the value is not available.
59+
*/
60+
val thirdValue: T
61+
get() = captor.thirdValue
62+
63+
/**
64+
* The last captured value of the argument.
65+
* @throws IndexOutOfBoundsException if the value is not available.
66+
*/
67+
val lastValue: T
68+
get() = captor.lastValue
69+
4470
val allValues: List<T>
4571
get() = captor.allValues
4672

4773
@Suppress("UNCHECKED_CAST")
4874
fun capture(): T = captor.capture() ?: createInstance(tClass) as T
4975
}
5076

77+
val <T> ArgumentCaptor<T>.firstValue: T
78+
get() = allValues[0]
79+
80+
val <T> ArgumentCaptor<T>.secondValue: T
81+
get() = allValues[1]
82+
83+
val <T> ArgumentCaptor<T>.thirdValue: T
84+
get() = allValues[2]
85+
86+
val <T> ArgumentCaptor<T>.lastValue: T
87+
get() = allValues.last()
88+
5189
/**
5290
* This method is deprecated because its behavior differs from the Java behavior.
5391
* Instead, use [argumentCaptor] in the traditional way, or use one of
@@ -58,3 +96,7 @@ inline fun <reified T : Any> capture(noinline consumer: (T) -> Unit): T {
5896
var times = 0
5997
return argThat { if (++times == 1) consumer.invoke(this); true }
6098
}
99+
100+
@Deprecated("Use captor.capture() instead.", ReplaceWith("captor.capture()"), DeprecationLevel.ERROR)
101+
inline fun <reified T : Any> capture(captor: KArgumentCaptor<T>): T = captor.capture()
102+

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

Lines changed: 43 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ class ArgumentCaptorTest {
1616
/* Then */
1717
val captor = argumentCaptor<Long>()
1818
verify(date).time = captor.capture()
19-
expect(captor.value).toBe(5L)
19+
expect(captor.lastValue).toBe(5L)
2020
}
2121

2222
@Test
@@ -30,7 +30,7 @@ class ArgumentCaptorTest {
3030
/* Then */
3131
val captor = argumentCaptor<String>()
3232
verify(m).nullableString(captor.capture())
33-
expect(captor.value).toBeNull()
33+
expect(captor.lastValue).toBeNull()
3434
}
3535

3636
@Test
@@ -44,7 +44,7 @@ class ArgumentCaptorTest {
4444
/* Then */
4545
val captor = nullableArgumentCaptor<String>()
4646
verify(m).nullableString(captor.capture())
47-
expect(captor.value).toBeNull()
47+
expect(captor.lastValue).toBeNull()
4848
}
4949

5050
@Test
@@ -76,4 +76,43 @@ class ArgumentCaptorTest {
7676
verify(m, times(2)).nullableString(captor.capture())
7777
expect(captor.allValues).toBe(listOf("test", null))
7878
}
79-
}
79+
80+
@Test
81+
fun argumentCaptor_callProperties() {
82+
/* Given */
83+
val m: Methods = mock()
84+
85+
/* When */
86+
m.int(1)
87+
m.int(2)
88+
m.int(3)
89+
m.int(4)
90+
m.int(5)
91+
92+
/* Then */
93+
argumentCaptor<Int>().apply {
94+
verify(m, times(5)).int(capture())
95+
96+
expect(firstValue).toBe(1)
97+
expect(secondValue).toBe(2)
98+
expect(thirdValue).toBe(3)
99+
expect(lastValue).toBe(5)
100+
}
101+
}
102+
103+
@Test(expected = IndexOutOfBoundsException::class)
104+
fun argumentCaptor_callPropertyNotAvailable() {
105+
/* Given */
106+
val m: Methods = mock()
107+
108+
/* When */
109+
m.int(1)
110+
111+
/* Then */
112+
argumentCaptor<Int>().apply {
113+
verify(m).int(capture())
114+
115+
expect(secondValue).toBe(2)
116+
}
117+
}
118+
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ interface Methods {
4949
fun closedStringMap(m: Map<Closed, String>)
5050
fun closedSet(s: Set<Closed>)
5151
fun string(s: String)
52+
fun int(i: Int)
5253
fun closedVararg(vararg c: Closed)
5354
fun throwableClass(t: ThrowableClass)
5455
fun nullableString(s: String?)

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -525,12 +525,12 @@ class CreateInstanceTest {
525525
throw AssertionError("Forbidden.")
526526
}
527527

528-
constructor(value: Int) {
528+
constructor(@Suppress("UNUSED_PARAMETER") value: Int) {
529529
}
530530
}
531531

532532
class WithDefaultEmptyConstructor() {
533-
constructor(c: ForbiddenConstructor) : this()
533+
constructor(@Suppress("UNUSED_PARAMETER") c: ForbiddenConstructor) : this()
534534
}
535535

536536
/**
@@ -549,7 +549,7 @@ class CreateInstanceTest {
549549
*/
550550
class WithDefaultParameters constructor(val first: Int = 1, val second: Int = 2) {
551551

552-
constructor(first: Int) : this() {
552+
constructor(@Suppress("UNUSED_PARAMETER") first: Int) : this() {
553553
error("Should not be called")
554554
}
555555
}

mockito-kotlin/src/testInlineMockito/kotlin/CreateInstanceInlineTest.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ class CreateInstanceInlineTest {
3333

3434
class ClassToBeMocked {
3535

36-
fun doSomething(c: ClassToBeMocked) {
36+
fun doSomething(@Suppress("UNUSED_PARAMETER") c: ClassToBeMocked) {
3737
}
3838

3939
fun doSomethingElse(value: BigInteger): BigInteger {

0 commit comments

Comments
 (0)