Skip to content

Commit

Permalink
Update to Mockito 3 and bump major version (#417)
Browse files Browse the repository at this point in the history
Mockito 3 requires Java 8 minimum and the ArgumentMatchers reject null
by default. Therefore, matchers will no longer work if null is passed in
(which is better for Kotlin since Kotlin is non-nullable by default).
  • Loading branch information
TimvdLippe authored Mar 29, 2021
1 parent 8031bd6 commit f4f4774
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 15 deletions.
2 changes: 1 addition & 1 deletion mockito-kotlin/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ dependencies {
compileOnly "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
compileOnly 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.0.0'

compile "org.mockito:mockito-core:2.23.0"
compile "org.mockito:mockito-core:3.8.0"

testCompile 'junit:junit:4.12'
testCompile 'com.nhaarman:expect.kt:1.0.0'
Expand Down
28 changes: 14 additions & 14 deletions mockito-kotlin/src/main/kotlin/org/mockito/kotlin/Matchers.kt
Original file line number Diff line number Diff line change
Expand Up @@ -27,36 +27,36 @@ package org.mockito.kotlin

import org.mockito.kotlin.internal.createInstance
import org.mockito.ArgumentMatcher
import org.mockito.Mockito
import org.mockito.ArgumentMatchers

/** Object argument that is equal to the given value. */
fun <T> eq(value: T): T {
return Mockito.eq(value) ?: value
return ArgumentMatchers.eq(value) ?: value
}

/** Object argument that is the same as the given value. */
fun <T> same(value: T): T {
return Mockito.same(value) ?: value
return ArgumentMatchers.same(value) ?: value
}

/** Matches any object, excluding nulls. */
inline fun <reified T : Any> any(): T {
return Mockito.any(T::class.java) ?: createInstance()
return ArgumentMatchers.any(T::class.java) ?: createInstance()
}

/** Matches anything, including nulls. */
inline fun <reified T : Any> anyOrNull(): T {
return Mockito.any<T>() ?: createInstance()
return ArgumentMatchers.any<T>() ?: createInstance()
}

/** Matches any vararg object, including nulls. */
inline fun <reified T : Any> anyVararg(): T {
return Mockito.any<T>() ?: createInstance()
return ArgumentMatchers.any<T>() ?: createInstance()
}

/** Matches any array of type T. */
inline fun <reified T : Any?> anyArray(): Array<T> {
return Mockito.any(Array<T>::class.java) ?: arrayOf()
return ArgumentMatchers.any(Array<T>::class.java) ?: arrayOf()
}

/**
Expand All @@ -66,7 +66,7 @@ inline fun <reified T : Any?> anyArray(): Array<T> {
* @param predicate An extension function on [T] that returns `true` when a [T] matches the predicate.
*/
inline fun <reified T : Any> argThat(noinline predicate: T.() -> Boolean): T {
return Mockito.argThat { arg: T? -> arg?.predicate() ?: false } ?: createInstance(
return ArgumentMatchers.argThat { arg: T? -> arg?.predicate() ?: false } ?: createInstance(
T::class
)
}
Expand All @@ -78,7 +78,7 @@ inline fun <reified T : Any> argThat(noinline predicate: T.() -> Boolean): T {
* @param matcher The ArgumentMatcher on [T] to be registered.
*/
inline fun <reified T : Any> argThat(matcher: ArgumentMatcher<T>): T {
return Mockito.argThat(matcher) ?: createInstance()
return ArgumentMatchers.argThat(matcher) ?: createInstance()
}

/**
Expand Down Expand Up @@ -107,33 +107,33 @@ inline fun <reified T : Any> argWhere(noinline predicate: (T) -> Boolean): T {
* Argument that implements the given class.
*/
inline fun <reified T : Any> isA(): T {
return Mockito.isA(T::class.java) ?: createInstance()
return ArgumentMatchers.isA(T::class.java) ?: createInstance()
}

/**
* `null` argument.
*/
fun <T : Any> isNull(): T? = Mockito.isNull()
fun <T : Any> isNull(): T? = ArgumentMatchers.isNull()

/**
* Not `null` argument.
*/
fun <T : Any> isNotNull(): T? {
return Mockito.isNotNull()
return ArgumentMatchers.isNotNull()
}

/**
* Not `null` argument.
*/
fun <T : Any> notNull(): T? {
return Mockito.notNull()
return ArgumentMatchers.notNull()
}

/**
* Object argument that is reflection-equal to the given value with support for excluding
* selected fields from a class.
*/
inline fun <reified T : Any> refEq(value: T, vararg excludeFields: String): T {
return Mockito.refEq<T>(value, *excludeFields) ?: createInstance()
return ArgumentMatchers.refEq<T>(value, *excludeFields) ?: createInstance()
}

0 comments on commit f4f4774

Please sign in to comment.