diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 500e97f2..24db84fc 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 30c3aea2..868260f7 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 631c5516..19f91a78 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 eq(value: T): T { @@ -51,7 +52,18 @@ inline fun anyOrNull(): T { /** Matches any vararg object, including nulls. */ inline fun anyVararg(): T { - return ArgumentMatchers.any() ?: createInstance() + return anyVararg(T::class) +} + +fun anyVararg(clazz: KClass): T { + return ArgumentMatchers.argThat(VarargMatcher(clazz.java))?: createInstance(clazz) +} + +private class VarargMatcher(private val clazz: Class) : ArgumentMatcher{ + 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 66dcabbb..6d190aa9 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 afed69f8..ac3021a5 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() { @@ -69,6 +68,14 @@ class MatchersTest : TestBase() { } } + @Test + fun anyVarargMatching() { + mock().apply { + whenever(varargBooleanResult(anyVararg())).thenReturn(true) + expect(varargBooleanResult()).toBe(true) + } + } + @Test fun anyNull_neverVerifiesAny() { mock().apply { @@ -277,7 +284,7 @@ class MatchersTest : TestBase() { /* Given */ val t = mock() // a matcher to check if any of the varargs was equals to "b" - val matcher = VarargAnyMatcher({ "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() // a matcher to check if any of the varargs was equals to "d" - val matcher = VarargAnyMatcher({ "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( private val match: ((T) -> Boolean), + private val clazz: Class, private val success: R, private val failure: R - ) : ArgumentMatcher, VarargMatcher, Answer { + ) : ArgumentMatcher, Answer { 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).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 +}