Shubham Suryavanshi
Shubham Suryavanshi

Reputation: 601

How to mock a method of kotlin object class using MockK

I've an kotlin object class like the one below

object DummyClass {

    fun method1() {}
}

and i want to mock the function method1() using MockK library

since this is an object class method of mocking a normal function not working on it, what i've tried is as shown below

also tried some other approaches but it didn't worked

@Test
fun method1Test() {
    val mock = mockk<DummyClass>()
    every { mock.method1() } just runs
    DummyClass.method1()
}

i've seen some questions here on StackOverflow, still haven't found any solution for my problem.

Upvotes: 3

Views: 8308

Answers (1)

Shubham Suryavanshi
Shubham Suryavanshi

Reputation: 601

To apply mocking on a Object class we can simply use mockkObject(ObjectClassName) and mocking method of that class is exactly same as for other method every { ObjectClassName.methodName() } just runs

After looking for solution over StackOverflow and Google

I found solution of my problem on official documentation of MockK

I've added a screen shot of my finding here, keeping this question hope it might help someone

enter image description here

Upvotes: 7

Related Questions