https://kotlinlang.org logo
#multiplatform
Title
# multiplatform
m

Mikhail Buzuverov

05/05/2021, 4:59 AM
Hello! Is there library for mocking common code? I have some classes in common code and I would like to write unit tests, so I need to mock their dependencies. Which library is suitable for that?
d

dephinera

05/05/2021, 12:53 PM
I use mockk.io
m

Mikhail Buzuverov

05/05/2021, 1:03 PM
But Mockk is for JVM only, isn't it?
d

dephinera

05/05/2021, 1:06 PM
Yes, but if the code you test is pure Kotlin without platform specific dependencies (different from JVM), you can run your tests on the JVM without an issue.
m

Mikhail Buzuverov

05/05/2021, 1:11 PM
But in real application at least part of common code will be executing in JS. So I feel right to check it too. For example, this way I found bug in serialization which won't work in JS
Test in JVM only is better than have no tests at all, of course
d

dephinera

05/05/2021, 1:14 PM
I think I didn't get what you're trying to achieve there. Do you want to test pure Kotlin components in the common code with unit tests our are you testing platform specific code that depend in the common code that you want to mock?
m

Mikhail Buzuverov

05/05/2021, 1:15 PM
I would like to mock platform-specific code and test common code.
d

dephinera

05/05/2021, 1:16 PM
Could you share a simple example, please
d

Diogo Ribeiro

05/05/2021, 4:47 PM
Why not to use the platform especifc then? You could test the common code and then test the platform especific
m

Mikhail Buzuverov

05/05/2021, 4:52 PM
If I use platform-specific mocks I have to write all unit tests twice - for JVM and for JS
d

dephinera

05/05/2021, 5:54 PM
Perhaps you could create an abstraction for the platform specific code, so you can mock it when you test the common part. Same thing for vice versa
3 Views