Hello, I have a trouble testing a class, that uses...
# multiplatform
d
Hello, I have a trouble testing a class, that uses platform specific API inside. I would like to test that both platforms behave identically. I have a
CustomFormatter
interface that uses NumberFormatter on android side which needs
withLocale
method to be mocked. I can make
NumberFormatter
a dependency of
AndroidCustomFormatter
but still
MocKMP
cannot mock
NumberFormatter
because it is not an interface. I could mock the whole CustomFormatter, but that kinda defeats the purpose. Is there any way around it? Thank you https://pl.kotl.in/-SpBAWQl-
s
Create a common test and one for each platform ie. https://pl.kotl.in/m68rt-zut
Maybe its not even needed to have that test case. Isnt what you have working ?
Its a bit difficult to know without having
Config
also
d
It is on ios, but on android it says:
Copy code
Method withLocale in android.icu.number.NumberFormatter not mocked.
Its a bit difficult to know without having
Config
also
There is actually no
Config
, I just pass some parameters regarding the formatting, like units the formatter should use, etc. I didn't wanna write the all out as they are really not relevant
Create a common test and one for each platform
I don't really understand how to make it work. Should I then override the
@Test fun
inside the subclass on android side?
s
Look at the buttom of my link
But I have doubts about your actual problem without the actual "Config" or its replacements
ie. it looks like its platform specific since you have
withLocale(config.locale)
d
Yeah, maybe I simplified it too much for sake of the minimal example. Hold up, ill expand it
Ok, I actually simplified it, because the way I pass
Locale
to the
CustomFormatter
is super complex and would require me to add multiple classes to the example just for that. Also the source of locale is irrelevant in this, the issue persists even with
Locale.ENGLISH
. I also removed the
Config
entirely as it just does not exist in my code. https://pl.kotl.in/yoeYRvCmK
s
And doesnt your example work ?
d
Nope, it does this:
It is on ios, but on android it says:
Copy code
Method withLocale in android.icu.number.NumberFormatter not mocked.
U mean that it does work for you?
Here is the example with all the implementation, I copied this into my project and it does not work. Running
IOSCustomFormatterTest
is fine, but running
AndroidCustomFormatterTest
crashes with
Copy code
Method withLocale in android.icu.number.NumberFormatter not mocked.
https://pl.kotl.in/OZIRgOKcV
s
Ok. That sounds like an android specific problem. Maybe this https://stackoverflow.com/a/75278020
d
Hmm, ok, I had to use mockito inside androidTest to be able to test my android code. Thanks
Ok, I did some more research and it turns out, that I was doing unit tests instead of instrumented tests. When doing instrumented tests on android, your solution works well. Thank you
s
Cheers!