I'm trying to write unit tests using mockk.
I'm trying to figure out how to mock a new instance of an object.
For example, using PowerMockito we would write:
PowerMockito.whenNew(Dog::class.java).withArguments("beagle").thenReturn(mockDog)
If the expected result of my test is mockDog, I want to be able to assert that it equals my actualResult:
assertEquals(mockDog, actualResult)
How would I accomplish this using mockk?
Thanks in advance.
Using mockkConstructor(Dog::class)
you can mock constructors in MockK. This will apply to all constructors for a given class, there is no way to distinguish between them.
The mocked class instance can be obtained by using anyConstructed<Dog>()
. You can use that to add any stubbing and verification you need, such as:
every { anyConstructed<Dog>().bark() } just Runs
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With