Skip to content

Commit b1d5e79

Browse files
authored
Upgrade mockito-core to 5.x (#482)
Fixes #478
1 parent 1ab7540 commit b1d5e79

File tree

5 files changed

+59
-19
lines changed

5 files changed

+59
-19
lines changed

.github/workflows/ci.yml

+6-6
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,10 @@ jobs:
3232
- name: 1. Check out code
3333
uses: actions/checkout@v2 # https://github.com/actions/checkout
3434

35-
- name: 2. Set up Java 8
35+
- name: 2. Set up Java 11
3636
uses: actions/setup-java@v1 # https://github.com/actions/setup-java
3737
with:
38-
java-version: 8
38+
java-version: 11
3939

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

63-
- name: 2. Set up Java 8
63+
- name: 2. Set up Java 11
6464
uses: actions/setup-java@v1 # https://github.com/actions/setup-java
6565
with:
66-
java-version: 8
66+
java-version: 11
6767

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

95-
- name: Set up Java 8
95+
- name: Set up Java 11
9696
uses: actions/setup-java@v1
9797
with:
98-
java-version: 8
98+
java-version: 11
9999

100100
- name: Build and release
101101
run: ./gradlew githubRelease publishToSonatype closeAndReleaseStagingRepository releaseSummary

mockito-kotlin/build.gradle

+10-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
2+
13
apply plugin: 'kotlin'
24
apply from: '../gradle/publishing.gradle'
35
apply plugin: 'org.jetbrains.dokka'
@@ -23,7 +25,7 @@ dependencies {
2325
compileOnly "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
2426
compileOnly 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.0.0'
2527

26-
compile "org.mockito:mockito-core:4.5.1"
28+
compile "org.mockito:mockito-core:5.3.1"
2729

2830
testCompile 'junit:junit:4.13.2'
2931
testCompile 'com.nhaarman:expect.kt:1.0.1'
@@ -45,4 +47,11 @@ dokka {
4547
suffix = "#L"
4648
}
4749
}
50+
51+
tasks.withType(KotlinCompile).configureEach {
52+
kotlinOptions {
53+
jvmTarget = JavaVersion.VERSION_11
54+
}
55+
}
56+
4857
javadoc.dependsOn dokka

mockito-kotlin/src/main/kotlin/org/mockito/kotlin/Matchers.kt

+14-2
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,10 @@
2525

2626
package org.mockito.kotlin
2727

28-
import org.mockito.kotlin.internal.createInstance
2928
import org.mockito.ArgumentMatcher
3029
import org.mockito.ArgumentMatchers
30+
import org.mockito.kotlin.internal.createInstance
31+
import kotlin.reflect.KClass
3132

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

5253
/** Matches any vararg object, including nulls. */
5354
inline fun <reified T : Any> anyVararg(): T {
54-
return ArgumentMatchers.any<T>() ?: createInstance()
55+
return anyVararg(T::class)
56+
}
57+
58+
fun <T : Any> anyVararg(clazz: KClass<T>): T {
59+
return ArgumentMatchers.argThat(VarargMatcher(clazz.java))?: createInstance(clazz)
60+
}
61+
62+
private class VarargMatcher<T>(private val clazz: Class<T>) : ArgumentMatcher<T>{
63+
override fun matches(t: T): Boolean = true
64+
65+
// In Java >= 12 you can do clazz.arrayClass()
66+
override fun type(): Class<*> = java.lang.reflect.Array.newInstance(clazz, 0).javaClass
5567
}
5668

5769
/** Matches any array of type T. */

tests/build.gradle

+10-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
2+
13
buildscript {
24
ext.kotlin_version = System.getenv("KOTLIN_VERSION") ?: '1.4.20'
35
println "$project uses Kotlin $kotlin_version"
@@ -21,8 +23,14 @@ dependencies {
2123
compile files("${rootProject.projectDir}/mockito-kotlin/build/libs/mockito-kotlin-${version}.jar")
2224

2325
compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
24-
compile "org.mockito:mockito-core:4.5.1"
26+
compile "org.mockito:mockito-core:5.3.1"
2527

2628
testCompile 'junit:junit:4.13.2'
2729
testCompile "com.nhaarman:expect.kt:1.0.1"
28-
}
30+
}
31+
32+
tasks.withType(KotlinCompile).configureEach {
33+
kotlinOptions {
34+
jvmTarget = JavaVersion.VERSION_11
35+
}
36+
}

tests/src/test/kotlin/test/MatchersTest.kt

+19-8
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,11 @@ import com.nhaarman.expect.expect
44
import com.nhaarman.expect.expectErrorWithMessage
55
import org.junit.Test
66
import org.mockito.ArgumentMatcher
7-
import org.mockito.internal.matchers.VarargMatcher
87
import org.mockito.invocation.InvocationOnMock
98
import org.mockito.kotlin.*
109
import org.mockito.stubbing.Answer
1110
import java.io.IOException
12-
import kotlin.check
11+
import kotlin.reflect.KClass
1312

1413
class MatchersTest : TestBase() {
1514

@@ -69,6 +68,14 @@ class MatchersTest : TestBase() {
6968
}
7069
}
7170

71+
@Test
72+
fun anyVarargMatching() {
73+
mock<Methods>().apply {
74+
whenever(varargBooleanResult(anyVararg())).thenReturn(true)
75+
expect(varargBooleanResult()).toBe(true)
76+
}
77+
}
78+
7279
@Test
7380
fun anyNull_neverVerifiesAny() {
7481
mock<Methods>().apply {
@@ -277,7 +284,7 @@ class MatchersTest : TestBase() {
277284
/* Given */
278285
val t = mock<Methods>()
279286
// a matcher to check if any of the varargs was equals to "b"
280-
val matcher = VarargAnyMatcher<String, Boolean>({ "b" == it }, true, false)
287+
val matcher = VarargAnyMatcher({ "b" == it }, String::class.java, true, false)
281288

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

296303
/* When */
297304
whenever(t.varargBooleanResult(argThat(matcher))).thenAnswer(matcher)
@@ -319,16 +326,20 @@ class MatchersTest : TestBase() {
319326
*/
320327
private class VarargAnyMatcher<T, R>(
321328
private val match: ((T) -> Boolean),
329+
private val clazz: Class<T>,
322330
private val success: R,
323331
private val failure: R
324-
) : ArgumentMatcher<T>, VarargMatcher, Answer<R> {
332+
) : ArgumentMatcher<T>, Answer<R> {
325333
private var anyMatched = false
326334

327335
override fun matches(t: T): Boolean {
328-
anyMatched = anyMatched or match(t)
329-
return true
336+
@Suppress("UNCHECKED_CAST") // No idea how to solve this better
337+
anyMatched = (t as Array<T>).any(match)
338+
return anyMatched
330339
}
331340

332341
override fun answer(i: InvocationOnMock) = if (anyMatched) success else failure
342+
343+
override fun type(): Class<*> = java.lang.reflect.Array.newInstance(clazz, 0).javaClass
333344
}
334-
}
345+
}

0 commit comments

Comments
 (0)