简体   繁体   中英

Mock a constructor and return a mocked object instead of real object with mockk

I have a challenge with a class which I want to test but inside the class other objects will be created.

This simple example shows the issue.

class A {
    val b: B
    init() {
      b = B()
    }
}

It's just an example and I know that dependency injection would help. But in real life it's a very complex class which can not be changed easily.

My idea was to use mockkConstructor . But it does not the trick.

fun `test construction`() {
    mockkConstructor(B::class)
    every { anyConstructed<B>() } returns mockk<B>()

    val a = A()
}

Unfortunately, it does not compile. Error: Missing mocked calls inside every {... } block: make sure the object inside the block is a mock

I tried it this way

fun `test construction`() {
    mockkConstructor(B::class)
    every { A() } returns mockk<B>()

    val a = A()
}

But this way it calls the real constructor of A and also the real constructor of B in the init method of A.

Does anybody know if it's possible to solve it this way or similar?

So I made a test for these example classes:

class A {
    val b: B
    init {
        b = B()
    }
}
class B {
    fun getA(): String {
        return "B"
    }
}

Test:

@Test
fun test() {
    mockkConstructor(A::class)
    val b = mockk<B>()
    every { anyConstructed<A>().b } returns b
    every { b.getA() } returns "MOCKK"

    val a = A()

    Assert.assertEquals("MOCKK", a.b.getA())
}

Your attempt to mock B() to return a mock of B is completely redundant after calling mockkConstructor(B::class) which already makes the constructor of B return a prototype mock denoted by anyConstructed<B>() .

This means, you only need anyConstructed<B>() to specify the behaviour of calls to that mock, eg

every { anyConstructed<B>().someFunctionCall() } returns 1

See MockK documentation: Constructor mocks for more details.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM