Skip to content

Commit

Permalink
Upgrade mockito-core to 5.x (#482)
Browse files Browse the repository at this point in the history
Fixes #478
  • Loading branch information
lukas-krecan authored Jun 1, 2023
1 parent 1ab7540 commit b1d5e79
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 19 deletions.
12 changes: 6 additions & 6 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@ jobs:
- name: 1. Check out code
uses: actions/checkout@v2 # https://github.com/actions/checkout

- name: 2. Set up Java 8
- name: 2. Set up Java 11
uses: actions/setup-java@v1 # https://github.com/actions/setup-java
with:
java-version: 8
java-version: 11

- name: 3. Validate Gradle wrapper
uses: gradle/wrapper-validation-action@v1 # https://github.com/gradle/wrapper-validation-action
Expand All @@ -60,10 +60,10 @@ jobs:
- name: 1. Check out code
uses: actions/checkout@v2 # https://github.com/actions/checkout

- name: 2. Set up Java 8
- name: 2. Set up Java 11
uses: actions/setup-java@v1 # https://github.com/actions/setup-java
with:
java-version: 8
java-version: 11

- name: 3. Build with Kotlin ${{ matrix.kotlin }} and mock-maker ${{ matrix.mock-maker }}
run: |
Expand Down Expand Up @@ -92,10 +92,10 @@ jobs:
with:
fetch-depth: '0' # https://github.com/shipkit/shipkit-changelog#fetch-depth-on-ci

- name: Set up Java 8
- name: Set up Java 11
uses: actions/setup-java@v1
with:
java-version: 8
java-version: 11

- name: Build and release
run: ./gradlew githubRelease publishToSonatype closeAndReleaseStagingRepository releaseSummary
Expand Down
11 changes: 10 additions & 1 deletion mockito-kotlin/build.gradle
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile

apply plugin: 'kotlin'
apply from: '../gradle/publishing.gradle'
apply plugin: 'org.jetbrains.dokka'
Expand All @@ -23,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:4.5.1"
compile "org.mockito:mockito-core:5.3.1"

testCompile 'junit:junit:4.13.2'
testCompile 'com.nhaarman:expect.kt:1.0.1'
Expand All @@ -45,4 +47,11 @@ dokka {
suffix = "#L"
}
}

tasks.withType(KotlinCompile).configureEach {
kotlinOptions {
jvmTarget = JavaVersion.VERSION_11
}
}

javadoc.dependsOn dokka
16 changes: 14 additions & 2 deletions mockito-kotlin/src/main/kotlin/org/mockito/kotlin/Matchers.kt
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,10 @@

package org.mockito.kotlin

import org.mockito.kotlin.internal.createInstance
import org.mockito.ArgumentMatcher
import org.mockito.ArgumentMatchers
import org.mockito.kotlin.internal.createInstance
import kotlin.reflect.KClass

/** Object argument that is equal to the given value. */
fun <T> eq(value: T): T {
Expand All @@ -51,7 +52,18 @@ inline fun <reified T : Any> anyOrNull(): T {

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

fun <T : Any> anyVararg(clazz: KClass<T>): T {
return ArgumentMatchers.argThat(VarargMatcher(clazz.java))?: createInstance(clazz)
}

private class VarargMatcher<T>(private val clazz: Class<T>) : ArgumentMatcher<T>{
override fun matches(t: T): Boolean = true

// In Java >= 12 you can do clazz.arrayClass()
override fun type(): Class<*> = java.lang.reflect.Array.newInstance(clazz, 0).javaClass
}

/** Matches any array of type T. */
Expand Down
12 changes: 10 additions & 2 deletions tests/build.gradle
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile

buildscript {
ext.kotlin_version = System.getenv("KOTLIN_VERSION") ?: '1.4.20'
println "$project uses Kotlin $kotlin_version"
Expand All @@ -21,8 +23,14 @@ dependencies {
compile files("${rootProject.projectDir}/mockito-kotlin/build/libs/mockito-kotlin-${version}.jar")

compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
compile "org.mockito:mockito-core:4.5.1"
compile "org.mockito:mockito-core:5.3.1"

testCompile 'junit:junit:4.13.2'
testCompile "com.nhaarman:expect.kt:1.0.1"
}
}

tasks.withType(KotlinCompile).configureEach {
kotlinOptions {
jvmTarget = JavaVersion.VERSION_11
}
}
27 changes: 19 additions & 8 deletions tests/src/test/kotlin/test/MatchersTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,11 @@ import com.nhaarman.expect.expect
import com.nhaarman.expect.expectErrorWithMessage
import org.junit.Test
import org.mockito.ArgumentMatcher
import org.mockito.internal.matchers.VarargMatcher
import org.mockito.invocation.InvocationOnMock
import org.mockito.kotlin.*
import org.mockito.stubbing.Answer
import java.io.IOException
import kotlin.check
import kotlin.reflect.KClass

class MatchersTest : TestBase() {

Expand Down Expand Up @@ -69,6 +68,14 @@ class MatchersTest : TestBase() {
}
}

@Test
fun anyVarargMatching() {
mock<Methods>().apply {
whenever(varargBooleanResult(anyVararg())).thenReturn(true)
expect(varargBooleanResult()).toBe(true)
}
}

@Test
fun anyNull_neverVerifiesAny() {
mock<Methods>().apply {
Expand Down Expand Up @@ -277,7 +284,7 @@ class MatchersTest : TestBase() {
/* Given */
val t = mock<Methods>()
// a matcher to check if any of the varargs was equals to "b"
val matcher = VarargAnyMatcher<String, Boolean>({ "b" == it }, true, false)
val matcher = VarargAnyMatcher({ "b" == it }, String::class.java, true, false)

/* When */
whenever(t.varargBooleanResult(argThat(matcher))).thenAnswer(matcher)
Expand All @@ -291,7 +298,7 @@ class MatchersTest : TestBase() {
/* Given */
val t = mock<Methods>()
// a matcher to check if any of the varargs was equals to "d"
val matcher = VarargAnyMatcher<String, Boolean>({ "d" == it }, true, false)
val matcher = VarargAnyMatcher({ "d" == it }, String::class.java, true, false)

/* When */
whenever(t.varargBooleanResult(argThat(matcher))).thenAnswer(matcher)
Expand Down Expand Up @@ -319,16 +326,20 @@ class MatchersTest : TestBase() {
*/
private class VarargAnyMatcher<T, R>(
private val match: ((T) -> Boolean),
private val clazz: Class<T>,
private val success: R,
private val failure: R
) : ArgumentMatcher<T>, VarargMatcher, Answer<R> {
) : ArgumentMatcher<T>, Answer<R> {
private var anyMatched = false

override fun matches(t: T): Boolean {
anyMatched = anyMatched or match(t)
return true
@Suppress("UNCHECKED_CAST") // No idea how to solve this better
anyMatched = (t as Array<T>).any(match)
return anyMatched
}

override fun answer(i: InvocationOnMock) = if (anyMatched) success else failure

override fun type(): Class<*> = java.lang.reflect.Array.newInstance(clazz, 0).javaClass
}
}
}

0 comments on commit b1d5e79

Please sign in to comment.