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

egor

03/30/2023, 2:10 PM
did someone try to use Mockk on KMM project?
h

Hylke Bron

03/30/2023, 2:19 PM
yes no sorry
n

Nicklas Jensen

03/30/2023, 2:19 PM
MockK does not support Kotlin/Native (and by extension part of Kotlin Multiplatform), and in its current form it cannot support it due to how it is implemented technically. About 2 years ago I decided to pick up the challenge to support mocking on Kotlin/Native and Kotlin Multiplatform which gave birth to Mockative, which allows you to mock `interface`s. While it has gained quite a bit of traction, it is not the only option. There is also MocKMP, although I have never used it myself personally.
h

Hylke Bron

03/30/2023, 2:20 PM
Oh i am mistaken, i use MocKMP 🙃
e

egor

03/30/2023, 2:20 PM
I just added dependencies, but it’s not resolved in Test classes
thanks, it makes sens
@Hylke Bron does MockKMP can mock the static? I want to mock Logger
h

Hylke Bron

03/30/2023, 2:22 PM
I am not sure, I dont have a requirement to mock statics, my project is set up without any statics involved
n

Nicklas Jensen

03/30/2023, 2:50 PM
Neither MocKMP nor Mockative can mock statics, and due to technical reasons it is currently very impractical to write a mocking library that supports mocking statics. It would have to be a Kotlin Compiler Plugin, which are not nearly as trivial to make.
You can refactor your code to inject an instance of a
Logger
interface which you can mock.
j

jw

03/30/2023, 2:51 PM
Fake, ideally. There's no reason to mock a logger.
n

Nicklas Jensen

03/30/2023, 2:52 PM
That I agree with @jw 😄
v

Vidmantas Kerbelis

03/31/2023, 6:01 AM
Using Mockk on my project. The only catch being I’m only testing
commonMain
. And running it from an Android (JVM) environment (same goes for the CI). On iOS’s side, - not running any tests
669 Views