diff --git a/mockito-kotlin/src/main/kotlin/org/mockito/kotlin/ArgumentCaptor.kt b/mockito-kotlin/src/main/kotlin/org/mockito/kotlin/ArgumentCaptor.kt index 253a58cd..0edfdb85 100644 --- a/mockito-kotlin/src/main/kotlin/org/mockito/kotlin/ArgumentCaptor.kt +++ b/mockito-kotlin/src/main/kotlin/org/mockito/kotlin/ArgumentCaptor.kt @@ -27,6 +27,7 @@ package org.mockito.kotlin import org.mockito.kotlin.internal.createInstance import org.mockito.ArgumentCaptor +import java.lang.reflect.Array import kotlin.reflect.KClass /** @@ -195,8 +196,21 @@ class KArgumentCaptor( @Suppress("UNCHECKED_CAST") fun capture(): T { - return captor.capture() ?: createInstance(tClass) as T + // Special handling for arrays to make it work for varargs + // In Kotlin we want have to capture vararg like this `verify(m).methodName(*captor.capture())` to make the types work + // If we return null for array types, the spread `*` operator will fail with NPE + // If we return empty array, it will fail in MatchersBinder.validateMatchers + // In Java, `captor.capture` returns null and so the method is called with `[null]` + // In Kotlin, we have to create `[null]` explicitly. + // This code-path is applied for non-vararg array arguments as well, but it seems to work fine. + return captor.capture() ?: if (tClass.java.isArray) { + singleElementArray() + } else { + createInstance(tClass) + } as T } + + private fun singleElementArray(): Any? = Array.newInstance(tClass.java.componentType, 1) } val ArgumentCaptor.firstValue: T diff --git a/tests/src/test/kotlin/test/ArgumentCaptorTest.kt b/tests/src/test/kotlin/test/ArgumentCaptorTest.kt index 703e7500..79c901a6 100644 --- a/tests/src/test/kotlin/test/ArgumentCaptorTest.kt +++ b/tests/src/test/kotlin/test/ArgumentCaptorTest.kt @@ -226,4 +226,88 @@ class ArgumentCaptorTest : TestBase() { } } } + + @Test + fun argumentCaptor_vararg() { + /* Given */ + val m: Methods = mock() + + /* When */ + m.varargBooleanResult("a", "b", "c") + + /* Then */ + val captor = argumentCaptor>() + verify(m).varargBooleanResult(*captor.capture()) + expect(captor.firstValue.toList()).toBe(listOf("a", "b", "c")) + } + + @Test + fun argumentCaptor_empty_vararg() { + /* Given */ + val m: Methods = mock() + + /* When */ + m.varargBooleanResult() + + /* Then */ + val captor = argumentCaptor>() + verify(m).varargBooleanResult(*captor.capture()) + expect(captor.firstValue.toList()).toBe(listOf()) + } + + @Test + fun argumentCaptor_arg_vararg() { + /* Given */ + val m: Methods = mock() + + /* When */ + m.argAndVararg("first", "a", "b", "c") + + /* Then */ + val captor = argumentCaptor>() + verify(m).argAndVararg(any(), *captor.capture()) + expect(captor.firstValue.toList()).toBe(listOf("a", "b", "c")) + } + + @Test + fun argumentCaptor_intarray() { + /* Given */ + val m: Methods = mock() + + /* When */ + m.intArray(intArrayOf(1, 2, 3)) + + /* Then */ + val captor = argumentCaptor() + verify(m).intArray(captor.capture()) + expect(captor.firstValue.toList()).toBe(listOf(1, 2, 3)) + } + + @Test + fun argumentCaptor_array() { + /* Given */ + val m: Methods = mock() + + /* When */ + m.stringArray(arrayOf("a", "b", "c")) + + /* Then */ + val captor = argumentCaptor>() + verify(m).stringArray(captor.capture()) + expect(captor.firstValue.toList()).toBe(listOf("a", "b", "c")) + } + + @Test + fun argumentCaptor_empty_array() { + /* Given */ + val m: Methods = mock() + + /* When */ + m.stringArray(arrayOf()) + + /* Then */ + val captor = argumentCaptor>() + verify(m).stringArray(captor.capture()) + expect(captor.firstValue.toList()).toBe(listOf()) + } } diff --git a/tests/src/test/kotlin/test/Classes.kt b/tests/src/test/kotlin/test/Classes.kt index 87b758da..c084b622 100644 --- a/tests/src/test/kotlin/test/Classes.kt +++ b/tests/src/test/kotlin/test/Classes.kt @@ -70,6 +70,8 @@ interface Methods { fun nullableStringResult(): String? fun builderMethod(): Methods fun varargBooleanResult(vararg values: String): Boolean + fun stringArray(a: Array) + fun argAndVararg(s: String, vararg a: String) fun nonDefaultReturnType(): ExtraInterface }