hi here, I am having a companion object, that decl...
# getting-started
g
hi here, I am having a companion object, that declares a method annotated with
JvmStatic
I want to write a junit/mockito test where I want to mock the method. Is there a way to do it? What if I remove
JvmStatic
? What are my options? I think it’s a part of the broader topic - how does the companion object relate to beans/singleton concepts of
Spring
, for example. Ideally, I would want no static methods at all. But instead having a singleton-y classes that I could mock or do whatever I can with the normal classes.
e
there is a #mockk channel fyi
❤️ 1
g
ah
thanks
e
but as for your question and edit: you want
mockkObject
on the companion object, and it does not relate
if you use top-level functions (not contained within objects) then you can
mockkStatic
them
(which don't exactly describe your scenario, but are applicable)
g
right, I will try that. My companion objects function is called from the java code. So the test resides also in the java code. I will try to work around it, thanks.
e
mockito doesn't handle Kotlin all that well, even with mockito-kotlin. there isn't really a great way to mock Kotlin types from Java
g
well, I guess if I remove
JvmStatic
, then there should be a way around. As it won’t be a static (from java point of view), so there should be a way to mock it. But then I would have to have it as a dependency.
Righ, I guess the only real option to me is to declare this function object as an explicit dependency of my java class, so that I could inject a mocked object.
That is actually what I meant by relation between companion objects and dependency injection frameworks. It looks to me like they try to achieve similar thing. Although di components are more explicit and thus versatile.
I can also re-write my test to Kotlin 😄
or try to at least
but thanks for thinking together
335 Views