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

gouravkundu

03/16/2018, 11:13 AM
how to mock companion object?
w

wickedev

03/16/2018, 11:28 AM
val companion = mock(Class.Companion::class)
with this solution. https://github.com/mockito/mockito/wiki/What%27s-new-in-Mockito-2#unmockable
r

rkeazor

03/16/2018, 2:32 PM
Have you actually tried this? because your are not injecting a dependency. So just because you create a val companion, what exactly would you do with it?
w

wickedev

03/16/2018, 5:24 PM
any problem?
r

rkeazor

03/18/2018, 1:33 PM
@wickedev yes there is a problem. How do you stub it out when its a dependency in another object
@wickedev Here let me show you
suppose I write this class
g

gildor

03/20/2018, 1:39 AM
Don’t mock companion object, that’s all. In 98% cases you can avoid it if use proper architecture solutions
@wickedev Your example is completely synthetic, hard to find case where it can be useful. Usually, when someone ask “how to mock companion object” it’s exactly the same case when people ask “how to mock static member”, so their code just not really testable (or testable with some heavy-weight mocking, like spy or final class mocking)
2014 Views