Skip to content

Commit

Permalink
Fix capturing varargs with Mockito 5 (#490)
Browse files Browse the repository at this point in the history
Fixes #474
  • Loading branch information
lukas-krecan authored Aug 7, 2023
1 parent d92b616 commit 3979104
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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

/**
Expand Down Expand Up @@ -195,8 +196,21 @@ class KArgumentCaptor<out T : Any?>(

@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 <T> ArgumentCaptor<T>.firstValue: T
Expand Down
84 changes: 84 additions & 0 deletions tests/src/test/kotlin/test/ArgumentCaptorTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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<Array<String>>()
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<Array<String>>()
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<Array<String>>()
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<IntArray>()
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<Array<String>>()
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<Array<String>>()
verify(m).stringArray(captor.capture())
expect(captor.firstValue.toList()).toBe(listOf())
}
}
2 changes: 2 additions & 0 deletions tests/src/test/kotlin/test/Classes.kt
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ interface Methods {
fun nullableStringResult(): String?
fun builderMethod(): Methods
fun varargBooleanResult(vararg values: String): Boolean
fun stringArray(a: Array<String>)
fun argAndVararg(s: String, vararg a: String)

fun nonDefaultReturnType(): ExtraInterface
}
Expand Down

0 comments on commit 3979104

Please sign in to comment.