@@ -52,9 +52,39 @@ inline fun <reified T : Any> anyVararg(): T = Mockito.any<T>() ?: createInstance
52
52
/* * Matches any array of type T. */
53
53
inline fun <reified T : Any ? > anyArray (): Array <T > = Mockito .any(Array <T >::class .java) ? : arrayOf()
54
54
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
+ */
56
69
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 )
58
88
59
89
fun atLeast (numInvocations : Int ): VerificationMode = Mockito .atLeast(numInvocations)!!
60
90
fun atLeastOnce (): VerificationMode = Mockito .atLeastOnce()!!
0 commit comments