https://kotlinlang.org logo
#mockk
Title
# mockk
l

Luiz Aguiar

03/20/2021, 11:30 AM
hello folks given the function:
Copy code
fun <T : Any> myFunction(foo: Foo, bar: Bar, response: KClass<T>): MyResponse<T> { do magic }
how can it be mocked to control the returned object? I tried something like:
Copy code
every { mockedClass.myFunction(any(), any(), ofType(SomeType::class) } returns SomeType()
but it didn't work... and using the type directly,
myFunction(any(), any(), SomeType::class)
is never matched.
e

ephemient

03/20/2021, 5:13 PM
why
ofType()
? that would match a
SomeType
, not a
KClass<SomeType>
a straightforward
Copy code
mockedClass.myFunction(any(), any(), SomeType::class)
should work
👍 1
if you do want
KClass<out SomeType>
, you'd have to write a custom matcher, like
Copy code
myFunction(any(), any(), match<KClass<*>>(SomeType::class::isSuperclassOf))
l

Luiz Aguiar

03/21/2021, 10:58 PM
@ephemient using
myFunction(any(), any(), SomeType::class)
is never matched… only returns
null
for it
3 Views