I'm trying to mock the constructor of a class, and define some behaviour on the methods of the constructed object. I'm using mockk to do this, and following their instructions to get it setup. However, I'm finding that when I try to define the mock behaviour, mockk is invoking the actual underlying method in the class, which fails because there are things missing on the mock.
Here's a minimal example
class TestConstructed() {
fun justThrows(): Int {
throw NotImplementedError()
}
}
class MinimalExample {
@Test
fun testMockConstructor() {
mockkConstructor(TestConstructed::class)
every { anyConstructed<TestConstructed>().justThrows() } returns 42
val constructed = TestConstructed()
assertThat(constructed.justThrows()).isEqualTo(42)
}
}
In the case above the call to justThrows inside every throws an exception, so the test never passes. What I want is to be able to mock the call to justThrows without the exception being thrown.
I'm using MockK 1.13.10 and running with JUnit 5.10.0
Thoughts?