diff options
author | Lukáš Křečan <lukas@krecan.net> | 2023-06-01 23:05:26 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-06-01 23:05:26 +0200 |
commit | b1d5e79fd7c78127b43a4b63714c772a33dd4097 (patch) | |
tree | f3f398538d9779ef52e1e5f8382dbe17bc4ddb1d | |
parent | 1ab75404f1f20abdbe0fa082438edfa4e9437ae2 (diff) | |
download | mockito-kotlin-b1d5e79fd7c78127b43a4b63714c772a33dd4097.tar.gz |
Upgrade `mockito-core` to 5.x (#482)
Fixes #478
-rw-r--r-- | .github/workflows/ci.yml | 12 | ||||
-rw-r--r-- | mockito-kotlin/build.gradle | 11 | ||||
-rw-r--r-- | mockito-kotlin/src/main/kotlin/org/mockito/kotlin/Matchers.kt | 16 | ||||
-rw-r--r-- | tests/build.gradle | 12 | ||||
-rw-r--r-- | tests/src/test/kotlin/test/MatchersTest.kt | 27 |
5 files changed, 59 insertions, 19 deletions
diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 500e97f..24db84f 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -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 @@ -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: | @@ -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 diff --git a/mockito-kotlin/build.gradle b/mockito-kotlin/build.gradle index 30c3aea..868260f 100644 --- a/mockito-kotlin/build.gradle +++ b/mockito-kotlin/build.gradle @@ -1,3 +1,5 @@ +import org.jetbrains.kotlin.gradle.tasks.KotlinCompile + apply plugin: 'kotlin' apply from: '../gradle/publishing.gradle' apply plugin: 'org.jetbrains.dokka' @@ -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' @@ -45,4 +47,11 @@ dokka { suffix = "#L" } } + +tasks.withType(KotlinCompile).configureEach { + kotlinOptions { + jvmTarget = JavaVersion.VERSION_11 + } +} + javadoc.dependsOn dokka diff --git a/mockito-kotlin/src/main/kotlin/org/mockito/kotlin/Matchers.kt b/mockito-kotlin/src/main/kotlin/org/mockito/kotlin/Matchers.kt index 631c551..19f91a7 100644 --- a/mockito-kotlin/src/main/kotlin/org/mockito/kotlin/Matchers.kt +++ b/mockito-kotlin/src/main/kotlin/org/mockito/kotlin/Matchers.kt @@ -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 { @@ -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. */ diff --git a/tests/build.gradle b/tests/build.gradle index 66dcabb..6d190aa 100644 --- a/tests/build.gradle +++ b/tests/build.gradle @@ -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" @@ -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" -}
\ No newline at end of file +} + +tasks.withType(KotlinCompile).configureEach { + kotlinOptions { + jvmTarget = JavaVersion.VERSION_11 + } +} diff --git a/tests/src/test/kotlin/test/MatchersTest.kt b/tests/src/test/kotlin/test/MatchersTest.kt index afed69f..ac3021a 100644 --- a/tests/src/test/kotlin/test/MatchersTest.kt +++ b/tests/src/test/kotlin/test/MatchersTest.kt @@ -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() { @@ -70,6 +69,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 { nullableString(null) @@ -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) @@ -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) @@ -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 } -}
\ No newline at end of file +} |