I have some mocks created using:
val someService = mockk<SomeService>(relaxed = true)
There are multiple tests in the file and I want the mock to be reset for each test
Is there currently a way to do this in MockK?
I know there is MockKAnnotations.init(this), but it didn't look like there was a way to set relaxed = true in the @Mock annotation
This is useful when the code under tests relies on the output of a mocked function. In that case, overriding the implementation allows us to create test cases that cover the relevant code paths. Given a function that returns a string based on the output of another function:
You use mocker by passing it as an argument to your test function, and calling the mock and patch functions from it. Say, you want the is_windows function to return True without taking those five precious seconds. We can patch it as follows:
Between test runs we need mocked/spied on imports and functions to be reset so that assertions don’t fail due to stale calls (from a previous test). This is a way to mitigate what little statefulness is in the system.
Mockito allows us to create a suitable test double for the CityRepository interface and lets us define the behavior we expect from it. Applying this possibility we can create meaningful unit tests to ensure the correct behavior of the service.
For resetting mocks in MockK you can use clearMocks
. To create relaxed mock via annotation just check @RelaxedMockK
clearAllMocks() clears all mocks without the need of specifying them.
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