https://kotlinlang.org logo
#android
Title
# android
o

Oleg Siboglov

04/29/2020, 8:52 PM
Hey everyone. Is there a simpler way to capture (Mockito) a single parameter of a Kotlin function that has multiple parameters, of which all have a default value.
Copy code
fun foo(int: Int = 0, string: String = "")
verify(someMock).foo(int = intCaptor.capture)
Currently Mockito is throwing a
InvalidUseOfMatchersException
, claiming its expecting 5 matchers (actual number of parameters) and instead it only recorded one. Im hoping theres a better approach than creating 5 different captors.
b

Brian Saltz Jr

04/29/2020, 9:10 PM
You should be able to mix captors with matchers, e.g.
any()
or
eq(value)
. You’d still have to provide all 5 parameters, but you don’t need to provide a captor for each one. You just can’t mix captors/matchers with actual values, which is what your
.foo()
call is doing in your example.
😅 1
o

Oleg Siboglov

04/29/2020, 9:12 PM
That’s so obvious. Thank you!
b

Brian Saltz Jr

04/29/2020, 9:12 PM
To be more clear, your example is compiling to:
Copy code
verify(someMock).foo(int = intCaptor.capture(), string = "")
So it’s mixing a real value (
""
) with a captor/matcher. You can do this to fix it:
Copy code
verify(someMock).foo(int = intCaptor.capture(), string = eq(""))
Or instead of
eq("")
, use
any()
if you truly don’t care if it’s using the default or not.
np, good luck 🙂
6 Views