Open westnordost opened 11 months ago
I started to look into this. Here are some preliminary research results:
Nice! Many thanks for the deep research! Very cool that you even found a bug in the (it looks like) otherwise seemingly most fitting library, and also created a PR. We will see how the maintainer handles these.
With Mockative, are only classes mockable that are annotated with @Mockable
?
There's a way to do it without the attribute, but it might not work. (Side note: the attribute is only necessary for all classes that are not open. But currently there are quite a lot of them in StreetComplete)
If you prefer to make all classes in your project open (and thus also mockable by Mockative) without annotating them individually, you can adjust the allOpen configuration to apply to all classes. However, use this approach with caution, as it may have unintended side effects by making all your classes non-final.
allOpen { annotation("kotlin.Metadata") }
I see, so maybe just using interfaces might be the cleanest solution. Even though it will lead to some boilerplate code, there is at least not black magic involved. (But this decision can certainly be postponed until the time when we actually have a KMP setup and migrate the tests from javaTest to commonTest. Theoretically, we could just keep all the tests in jvmTests, too)
The comment in https://github.com/mockative/mockative/pull/100#issuecomment-2314458235 of the main maintainer of Mockative is sadly not inspiring confidence in the continued development and support of Mockative for Kotlin 2.
Mokkery has seen a lot of development in the last months and looks promising in this regard. And it already has support for Kotlin 2.
But I still have to check it out and see if it has all the features we need for the tests.
Hm, this is sad, as you've put so much effort into integration with Mockative already. Both Mockative and MocKMP seem to have the same issue with K2. I do wonder whether the maintainers of these two libraries know of Mokkery? Maybe they want to take a similar approach with making their libraries K2-compatible. Then on the other hand, I don't know how bug such a mocking library is. Maybe if they would do that, they would just end up having a Mokkery in green.
The maintainer of Mockative knows of Mokkery. They commented about it somewhere in the Mockative issues, that's how I found Mokkery in the first place. They also commented on a Mokkery ticket about recursive / chain mocking.
MockKMP seems to be dead / unmaintained.
For mocking unit tests, we are currently using the Java library
org.mockito:mockito
.In order that the tests for platform-independent application logic can be executed on any supported platform, mockito needs to be replaced as a test dependency.
Mocking library requirements
multiplatform support for at least Java/Android and iOS
stable, well-tested and ideally (semi-)official / well-maintained
support all features of Mockito we are using now
It is possible that when migrating, a bit more effort is necessary because as far as I know, Mockito uses Java reflection to create the mocks, which is a Java specific language feature. In other words, it may not be possible to mock non-interfaces.
Three Kotlin Multiplatform libraries for mocking are known to me, with a few comments from a short look at them, ordered by popularity on github:
Mockative
MocKMP
MockingBird