Reputation: 1313
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.
Upvotes: 10
Views: 11053
Reputation: 842
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
Upvotes: 4