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

Michael Vandendriessche

03/02/2022, 3:26 PM
Hello everyone, What do I return when I don't care about the return value? I can't return unit since the return type is
SomeClass
. Do I have to
return SomeClass()
or is there something else available like return any or return whatever?
m

Mattia Tommasone

03/02/2022, 3:29 PM
off the top of my head i’d say you can use a relaxed mock. Can you please provide a full example of your problem though?
m

Michael Vandendriessche

03/02/2022, 3:34 PM
I'm testing a function with this line
Copy code
batteryImage.setImageDrawable(context.getResources().getDrawable(R.drawable.battery_100));
I'm mocking
getDrawable
, I don't care what it returns I just want to verify it was called with R.drawable.battery_100. And I do not want to execute the getresources and getdrawable. I know it's not ideal code to unit test but it's what I have right now. Fixing these thingsi s on the planning for later. Thanks for your reply!
c

christophsturm

03/02/2022, 3:40 PM
you can either use
justRun
or use a relaxed mock.
m

Michael Vandendriessche

03/02/2022, 3:48 PM
@christophsturm ah yes instead of
every
? Sorry I forgot about that one, I was trying
just Runs
as return value. Thanks!
👍 1
c

christophsturm

03/02/2022, 3:49 PM
I think just run as return value should also work
m

Michael Vandendriessche

03/02/2022, 3:49 PM
just
only works on Unit
m

Mattia Tommasone

03/02/2022, 3:53 PM
yep that’s right
just runs
can only be used on methods returning Unit. i’d say your best option is to use a relaxed mock
m

Michael Vandendriessche

03/02/2022, 4:09 PM
I'm happy with the justRun, I will consider relaxed mock when I have more cases like this. Thanks!
👍 1
8 Views