Skip to content

Commit 327d431

Browse files
authored
Merge pull request mockito#137 from nhaarman/release-1.0.1
Release 1.0.1
2 parents e07e419 + 8c9e5d7 commit 327d431

File tree

3 files changed

+72
-3
lines changed

3 files changed

+72
-3
lines changed

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

+32-2
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,39 @@ inline fun <reified T : Any> anyVararg(): T = Mockito.any<T>() ?: createInstance
5252
/** Matches any array of type T. */
5353
inline fun <reified T : Any?> anyArray(): Array<T> = Mockito.any(Array<T>::class.java) ?: arrayOf()
5454

55-
inline fun <reified T : Any> argThat(noinline predicate: T.() -> Boolean) = Mockito.argThat<T> { it -> (it as T).predicate() } ?: createInstance(T::class)
55+
/**
56+
* Creates a custom argument matcher.
57+
* `null` values will never evaluate to `true`.
58+
*
59+
* @param predicate An extension function on [T] that returns `true` when a [T] matches the predicate.
60+
*/
61+
inline fun <reified T : Any> argThat(noinline predicate: T.() -> Boolean) = Mockito.argThat<T?> { arg -> arg?.predicate() ?: false } ?: createInstance(T::class)
62+
63+
/**
64+
* Creates a custom argument matcher.
65+
* `null` values will never evaluate to `true`.
66+
*
67+
* @param predicate An extension function on [T] that returns `true` when a [T] matches the predicate.
68+
*/
5669
inline fun <reified T : Any> argForWhich(noinline predicate: T.() -> Boolean) = argThat(predicate)
57-
inline fun <reified T : Any> check(noinline predicate: (T) -> Unit) = Mockito.argThat<T> { it -> predicate(it); true } ?: createInstance(T::class)
70+
71+
/**
72+
* For usage with verification only.
73+
*
74+
* For example:
75+
* verify(myObject).doSomething(check { assertThat(it, is("Test")) })
76+
*
77+
* @param predicate A function that performs actions to verify an argument [T].
78+
*/
79+
inline fun <reified T : Any> check(noinline predicate: (T) -> Unit) = Mockito.argThat<T?> { arg ->
80+
if (arg == null) error("""The argument passed to the predicate was null.
81+
82+
If you are trying to verify an argument to be null, use `isNull()`.
83+
If you are using `check` as part of a stubbing, use `argThat` or `argForWhich` instead.
84+
""".trimIndent())
85+
predicate(arg)
86+
true
87+
} ?: createInstance(T::class)
5888

5989
fun atLeast(numInvocations: Int): VerificationMode = Mockito.atLeast(numInvocations)!!
6090
fun atLeastOnce(): VerificationMode = Mockito.atLeastOnce()!!

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

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
package test/*
1+
package test
2+
3+
/*
24
* The MIT License
35
*
46
* Copyright (c) 2016 Niek Haarman
@@ -55,6 +57,7 @@ interface Methods {
5557
fun nullableString(s: String?)
5658

5759
fun stringResult(): String
60+
fun stringResult(s: String): String
5861
fun nullableStringResult(): String?
5962
fun builderMethod(): Methods
6063
}

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

+36
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,17 @@ class MockitoTest : TestBase() {
138138
}
139139
}
140140

141+
@Test
142+
fun checkWithNullArgument_throwsError() {
143+
mock<Methods>().apply {
144+
nullableString(null)
145+
146+
expectErrorWithMessage("null").on {
147+
verify(this).nullableString(check {})
148+
}
149+
}
150+
}
151+
141152
@Test
142153
fun atLeastXInvocations() {
143154
mock<Methods>().apply {
@@ -458,6 +469,30 @@ class MockitoTest : TestBase() {
458469
}
459470
}
460471

472+
@Test
473+
fun stubbingTwiceWithArgumentMatchers() {
474+
/* When */
475+
val mock = mock<Methods> {
476+
on { stringResult(argThat { this == "A" }) } doReturn "A"
477+
on { stringResult(argThat { this == "B" }) } doReturn "B"
478+
}
479+
480+
/* Then */
481+
expect(mock.stringResult("A")).toBe("A")
482+
expect(mock.stringResult("B")).toBe("B")
483+
}
484+
485+
@Test
486+
fun stubbingTwiceWithCheckArgumentMatchers_throwsException() {
487+
/* Expect */
488+
expectErrorWithMessage("null").on {
489+
mock<Methods> {
490+
on { stringResult(check { }) } doReturn "A"
491+
on { stringResult(check { }) } doReturn "B"
492+
}
493+
}
494+
}
495+
461496
@Test
462497
fun doReturn_withSingleItemList() {
463498
/* Given */
@@ -500,6 +535,7 @@ class MockitoTest : TestBase() {
500535
verify(this).string(isA<String>())
501536
}
502537
}
538+
503539
@Test
504540
fun isA_withNullableString() {
505541
mock<Methods>().apply {

0 commit comments

Comments
 (0)