Can we mock constructor and check its parameters?

Viewed 9460

With mockk, to mock constructors, we can do something like (taken from documentation):

class MockCls {
  fun add(a: Int, b: Int) = a + b
}

mockkConstructor(MockCls::class)

every { anyConstructed<MockCls>().add(1, 2) } returns 4

assertEquals(4, MockCls().add(1, 2)) // note new object is created

verify { anyConstructed<MockCls>().add(1, 2) }

I'd like to check on the parameters of my constructor. Something like:

class MockCls(val minValue: Int) {
  fun add(a: Int, b: Int) = minValue + a + b
}

mockkConstructor(MockCls::class)

every { anyConstructed<MockCls>(10).add(1, 2) } returns 14

assertEquals(14, MockCls(10).add(1, 2)) // note new object is created

verify { anyConstructed<MockCls>(10).add(1, 2) } // success
verify { anyConstructed<MockCls>(5).add(1, 2) } // fail

I didn't find any way to check constructor parameters right now.

1 Answers

Unfortunately, it is currently not possible, as shown in the feature request you kindly made.

Related