how to mock companion object?
# android
g
how to mock companion object?
w
val companion = mock(Class.Companion::class)
with this solution. https://github.com/mockito/mockito/wiki/What%27s-new-in-Mockito-2#unmockable
r
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
any problem?
r
@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
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)
2143 Views